'Setup separate test data for each test method in a class
I'm writing an integration test with MSTest. The methods I want to test in a class are: method1 that extracts some metadata from a file and method2 renames a few files in a directory. I need a certain test data for method1 and other test data for method2. The test data is real files that will be copied to a test folder and deleted after the test is done.
I thought of two ways:
Option1
Copy files for method1 to test/method1_data and copy files for method2 to test/method2_data. Then, the test methods will access corresponding directory during the test.
[TestClass]
public class MyTests
{
[ClassInitialize]
public void Setup()
{
// unzip test files to "test/method1_data" and "test/method2_data"
}
[ClassCleanup]
public void Cleanup()
{
// delete test files
}
[TestMethod]
public void TestMethod1()
{
// use files from "test/method1_data"
}
[TestMethod]
public void TestMethod2()
{
// use files from "test/method2_data"
}
}
Option2
Have two test classes, each tests just one method
[TestClass]
public class MyTests1
{
[ClassInitialize]
public void Setup()
{
// unzip test files to "test/method1_data"
}
[ClassCleanup]
public void Cleanup()
{
// delete test files
}
[TestMethod]
public void TestMethod1()
{
// use files from "test/method1_data"
}
}
[TestClass]
public class MyTests2
{
[ClassInitialize]
public void Setup()
{
// unzip test files to "test/method2_data"
}
[ClassCleanup]
public void Cleanup()
{
// delete test files
}
[TestMethod]
public void TestMethod2()
{
// use files from "test/method2_data"
}
}
What is more preferable option?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
