如何使用 Visual Studio 来排序执行方法来进行集成测试?

发布于 2024-10-11 14:18:03 字数 442 浏览 1 评论 0原文

我有两个关于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-10-18 14:18:03

要按特定顺序执行测试,我遵循以下步骤:

在包含 test1、test2 和 test3 的测试项目中

1 右键单击​​项目“添加”->“新测试...”
2 选择“订购测试”
3 双击出现的文件“OrderedTest1.orderedtest”

alt text

4 如果未构建项目,则构建项目以前
5 从可用测试列表中选择您想要的测试并订购它们

alt text

从那时起,将出现测试列表编辑器中的新测试

alt text

这是一个额外的测试,以正确的方式运行随附的测试顺序,但如果您不小心运行项目中的所有测试,则有序列表中包含的测试将执行两次,因此您需要以某种方式管理列表或测试类别以避免这种情况。
我尝试禁用单个测试,但这也禁用了有序测试,我不知道更好的方法。

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"

alt text

4 Build the project if was not build previously
5 From the list of available test select the test you want and order them

alt text

From that point on there appears a new test in the test list editor

alt text

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.

感性 2024-10-18 14:18:03

最佳实践是使用函数来设置测试并通过使用属性 [TestInitialize] 和 [TestCleanUp] 或 [ClassInitialize] 和 [ClassCleanup] 来清理它们。
http://msdn。 microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting(v=VS.100).aspx

下一个代码是与您想要的类似的示例:

[TestClass]
public class UnitTest1
{
    int i=0;

    [TestInitialize]
    public void Setup()
    {
        i = 5;
    }

    [TestMethod]
    public void TestMethod1()
    {
        Assert.AreEqual(5, i);
    }
}

在执行每个测试之前将调用函数 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:

[TestClass]
public class UnitTest1
{
    int i=0;

    [TestInitialize]
    public void Setup()
    {
        i = 5;
    }

    [TestMethod]
    public void TestMethod1()
    {
        Assert.AreEqual(5, i);
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文