visual-studio-2010 - VS 2010 : Pass results of a TestMethod to another Testmethod

标签 visual-studio-2010 testing

我有两个 [TestMethods]。 MethodA的结果需要作为MethodB的输入。问题是当一个新的测试方法开始时,所有的值和变量都会被重置。

已经有人问过 exact same question , 但还没有真正的解决方案。

我想要的只是下面的工作:

Guid CustomerID;

[TestMethod]
public void CreateCustomer()
{   
    // Create a new customer and store the customer id   
    CustomerID = CreateNewCustomer();
    Assert.IsNotNull(...);
}

[TestMethod]
public void DeleteCustomer()
{   
    // Delete the customer created before   
    var ok = DeleteCustomer(CustomerID);   
    Assert.IsNotNull(...);
}

我知道这不是测试的“官方”方式,但我确实需要针对这种情况的实用解决方案 - 所以我希望有某种解决方法。

有什么想法吗?

最佳答案

为什么不在删除客户测试中创建客户?

 [TestMethod]
 public void CreateCustomer()
 {   
     // Create a new customer and store the customer id   
     var customerID = CreateNewCustomer();
     Assert.IsNotNull(...);
 }

[TestMethod]
public void DeleteCustomer()
{   
    // Delete the customer created before          
    var customerID = CreateNewCustomer(); 
    var ok = DeleteCustomer(customerID);   
    Assert.IsNotNull(...);   
}

或者只是在测试夹具设置中创建客户:

(TestFixtureSetUp 的名称在 VS 测试环境中可能会有所不同,这是在 NUnit 中的名称,但会有一个等效名称)

private Guid CustomerID;

[TestFixtureSetUp]
{
    **EDIT** you could ensure you DB is clean here:
    CleanDB();
    CustomerID = CreateNewCustomer();
}


[TestMethod]
public void CreateCustomer()
{   
    // check previously created customer           
    Assert.IsNotNull(...);
}

[TestMethod]
public void DeleteCustomer()
{   
    // Delete the customer created before          
    var ok = DeleteCustomer(CustomerID);   
    Assert.IsNotNull(...);
}

[TestFixtureTearDown]
{
    **EDIT** or you could ensure you DB is clean here:
    CleanDB();
}

在我看来,第一个解决方案更好,因为每个测试都负责创建自己的数据,但如果这是一个集成测试,它实际上是将东西放入和取出数据库,那么它就可以了(同样在我看来)在该类的设置中获得所有测试所需的数据,然后所有测试都可以运行,期望数据在那里。您应该确保每个测试类也有相应的测试拆卸,这将从数据库中删除此类测试数据,或者您在运行每个测试类之前在某处清理数据库(例如在公共(public)基类中)

关于visual-studio-2010 - VS 2010 : Pass results of a TestMethod to another Testmethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5311543/

相关文章:

testing - 在顽固的测试套件中使用包含随机数的 .txt 文件

c# - 带有 SQL 的 Visual Studio 2010 探查器

visual-studio-2010 - 使 "Go to definition"导航到 .NET 引用源

visual-studio - 如何在VS2010中的 header 和实现之间切换?

testing - 在非主程序包中运行类似主程序的程序

.NET 窗体上 Web 控制的 Ruby 自动化测试

testing - 如何使用 Protractor js单击表格中行中的按钮

c# - 在 Visual Studio 中通过中间语言 (IL) 和 C# 同时进行调试

c++ - Winsock 重新定义错误

api - 在不实际访问第三方 API 的情况下测试 Go (Golang) API 传出请求