VS 2010: Pass results of a TestMethod to another Testmethod
I have two [TestMethods]. The result of MethodA needs to be the input of MethodB开发者_如何学C. The problem is that all values and variables are reset when a new test method starts.
Somebody has already asked the exact same question, but there was no real solution yet.
All I want is the following to work:
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(...);
}
I understand that this is not the "official" way for testing, but I really need a practical solution for this scenario -so I am hoping for some kind of workaround.
Any ideas?
Why not create the customer in your delete customer test?
[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(...);
}
or just create the customer in the testfixture set up:
(The name of the TestFixtureSetUp might be different in VS test environment, that is what it is called in NUnit, but there will be an equivalent)
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();
}
The first solution is better in my opinion as each test is responsible for creating its own data, but if this is an integration test which is actually putting stuff into and out of the database then it is ok (again in my opinion) to have the data needed for all tests to be done in the setup for that class and then all tests can run expecting the data to be there. You should ensure though that each test class also has a corresponding test tear down which will remove this classes test data from the db, or that you are cleaning the DB somewhere before each test class is run (like in a common base class)
You need to use [TestInitialize] method for test prerequisities as in your case for creating new customer because each [TestMethod] is ran standalone.
精彩评论