如何使用 Visual Studio 来排序执行方法来进行集成测试?
我有两个关于使用 VS 2010 进行集成测试的问题
首先,我真的需要找到一种方法来按照我想要的顺序执行这些测试方法。注意:我知道在单元测试中,方法应该独立于其他任何东西运行,但这些是集成测试,我确实依赖于哪个方法首先运行的顺序。
同样,有没有办法通过运行测试来保留局部变量?例如,下面的代码现在失败了。
[TestClass]
public class UnitTest1
{
int i = 0;
[TestMethod]
public void TestMethod1()
{
i = 5;
}
[TestMethod]
public void TestMethod2()
{
Assert.AreEqual(5, i);
}
}
那么有没有办法做到这些呢?
I have 2 questions in regard doing integration testing using VS 2010
First, I'm really in need of finding a way to execute these testing methods in the order I want them to. Note: I know in Unit Testing, methods should run standalone from anything else, but these are integration tests which I do depend on the order of which method runs first.
On the same note, is there a way to keep a local variable through running the tests? For example like the following code which right now fails.
[TestClass]
public class UnitTest1
{
int i = 0;
[TestMethod]
public void TestMethod1()
{
i = 5;
}
[TestMethod]
public void TestMethod2()
{
Assert.AreEqual(5, i);
}
}
So is there a way to do any of these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要按特定顺序执行测试,我遵循以下步骤:
在包含 test1、test2 和 test3 的测试项目中
1 右键单击项目“添加”->“新测试...”
2 选择“订购测试”
3 双击出现的文件“OrderedTest1.orderedtest”
4 如果未构建项目,则构建项目以前
5 从可用测试列表中选择您想要的测试并订购它们
从那时起,将出现测试列表编辑器中的新测试
这是一个额外的测试,以正确的方式运行随附的测试顺序,但如果您不小心运行项目中的所有测试,则有序列表中包含的测试将执行两次,因此您需要以某种方式管理列表或测试类别以避免这种情况。
我尝试禁用单个测试,但这也禁用了有序测试,我不知道更好的方法。
To execute tests in a specific order I followed the next steps:
In a test project with test1, test2 and test3
1 Right click on the project 'Add'->'new test..."
2 Select 'Ordered Test'
3 Double click in the file that appears "OrderedTest1.orderedtest"
4 Build the project if was not build previously
5 From the list of available test select the test you want and order them
From that point on there appears a new test in the test list editor
It is an extra test that runs the enclosed tests in the correct order, but if you run all the test in the project carelessly the tests included in the ordered list will be executed twice so you need to somehow manage lists or test categories to avoid that.
I tried disabling the individual tests but that also disables the ordered test, I don't know a better way to do so.
最佳实践是使用函数来设置测试并通过使用属性 [TestInitialize] 和 [TestCleanUp] 或 [ClassInitialize] 和 [ClassCleanup] 来清理它们。
http://msdn。 microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting(v=VS.100).aspx
下一个代码是与您想要的类似的示例:
在执行每个测试之前将调用函数 SetUp。
如果您需要将值从一个测试传递到另一个测试,您可能需要考虑使用静态变量,由于执行顺序不确定,不建议使用静态变量。
通常有一种方法可以通过使用设置/清理技术来避免需要特定的顺序,但对于非常复杂的集成测试来说,这可能并非如此。
如果没有可能的方法来避免它们重新排序,您可以考虑将它们合并为一个,再次打破每个测试只有一个断言的最佳实践,但如果它们彼此之间有如此多的依赖,那么这可能会更好方式,因为在这种情况下,一个测试失败可能会影响其他测试的结果。
编辑:
可能使用有序测试回答问题 1,并使用静态变量回答问题 2:
http://msdn.microsoft.com/en-us/library/ms182631.aspx
It is best practice to use functions to set up the tests and to clean them up, by using the attributes [TestInitialize] and [TestCleanUp] or [ClassInitialize] and [ClassCleanup].
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting(v=VS.100).aspx
The next code is an example of a similar thing to what you want:
The function SetUp will be called before executing each test.
If you need to pass the value from one test to the other you might want to consider using a static variable which is not recommended due to the indeterministic order of execution.
Usually there is a way to avoid needing a specific order by using the setup/cleanup technique, but it is true that this might not be true for very complex integration tests.
If there is no possible way to avoid having them to reorder you can consider merging them in one, breaking again the best practice of having only one assert per test, but if they are so much dependent one from the other it might be even better this way, as in this case one test failing might compromise the result of the others.
EDIT:
May be using ordered tests answers question 1, and using static variables question 2:
http://msdn.microsoft.com/en-us/library/ms182631.aspx