Visual Studio 2008 中单元测试的执行顺序

发布于 2024-08-14 07:16:03 字数 732 浏览 5 评论 0 原文

我为我的 Visual Studio 2008 解决方案定义了单元测试。这些测试是在多个文件中的多个方法和多个类中定义的。

我读过博客文章 当使用 MSTest 时,认为可以依赖测试的执行顺序是错误的:

执行交错:由于测试类的每个实例都是在不同的线程上单独实例化的,因此无法保证 关于单个类中单元测试的执行顺序,或者 跨班级。测试的执行可以交叉进行 类,甚至可能是程序集,具体取决于您的选择 执行您的测试。这里的关键是——所有测试都可以 以任何顺序执行,它是完全未定义的。

也就是说,在运行这些测试之前,我必须有一个预执行步骤。也就是说,我实际上想以某种方式定义执行顺序。例如,1)首先创建数据库; 2)测试它是否已创建;然后 3) 以任意顺序运行剩余的 50 个测试。

关于我如何做到这一点有什么想法吗?

I have unit tests defined for my Visual Studio 2008 solution. These tests are defined in multiple methods and in multiple classes across several files.

I've read in a blog article that when using MSTest, it is a mistake to think that you can depend on the order of execution of your tests:

Execution Interleaving: Since each instance of the test class is instantiated separately on a different thread, there are no guarantees
regarding the order of execution of unit tests in a single class, or
across classes. The execution of tests may be interleaved across
classes, and potentially even assemblies, depending on how you chose
to execute your tests. The key thing here is – all tests could be
executed in any order, it is totally undefined.

That said, I have to have a pre-execution step before any of these tests gets to run. That is, I actually want to define an order of execution somehow. For example, 1) first create the database; 2) test that it's created; then 3) run the remaining 50 tests in arbitrary order.

Any ideas on how I can do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

吃兔兔 2024-08-21 07:16:03

我不会测试数据库是否已成功创建;如果不是这样,我将假设所有后续测试都会失败,并且在某种程度上感觉您将测试测试代码。

关于设置数据库的预测试步骤,您可以通过创建一个方法并使用 ClassInitialize 属性。这将使测试框架在测试类中的任何其他方法之前执行该方法:

[ClassInitialize()]
public static void InitializeClass(TestContext testContext) 
{ 
    // your init code here
}

I wouldn't test that the database is successfully created; I will assume that all subsequent tests will fail if it is not, and it feels in a way that you would be testing the test code.

Regarding a pre-test step to set up the database, you can do that by creating a method and decorating it with the ClassInitialize attribute. That will make the test framework execute that method prior to any other method within the test class:

[ClassInitialize()]
public static void InitializeClass(TestContext testContext) 
{ 
    // your init code here
}
深海夜未眠 2024-08-21 07:16:03

单元测试应该全部独立工作,并且不应相互依赖,否则您无法单独运行单个测试。

每个需要数据库的测试都应该按需创建它(如果尚未创建 - 您可以使用单例/静态类来确保如果批量执行多个测试,则数据库实际上只创建一次)。

那么先执行哪个测试就不再重要了;它只会在测试第一次需要使用数据库时创建。

Unit tests should all work standalone, and should not have dependencies on each other, otherwise you can't run a single test in isolation.

Every test that needs the database should then just create it on demand (if it's not already been created - you can use a singleton/static class to ensure that if multiple tests are executed in a batch, the database is only actually created once).

Then it won't matter which test executes first; it'll just be created the first time a test needs a database to use.

一页 2024-08-21 07:16:03

理论上,测试应该相互独立并且能够独立运行是正确的。但在实践中,理论和实践之间存在差异,VS2010 的固定执行顺序(始终相同的随机顺序)让我很为难。

以下是一些示例:
我有一个单元测试,交叉检查一些表之间的日期并验证一切是否一致。显然,在空数据库上运行此测试是没有用的,因此我想在插入数据的单元测试之后运行一段时间。抱歉,VS2010 不允许您这样做。
好的,酷,那么我将把它作为尾声添加到插入单元测试中。但后来我想交叉检查其他 10 件事,而不是进行单元测试(“确保可以插入具有各种参数的实体而不会崩溃”),我最终进行了一次大型测试。

然后是另一个案例。
我的单元测试插入实体,只需插入即可,以确保这部分逻辑工作正常。然后我有一个多线程版本的测试,以确保没有死锁之类的东西。显然,我需要在单线程测试之后运行多线程测试一段时间,并且仅当单线程测试成功时才运行。抱歉,VS2010 无法做到这一点。

另一个案例。我有一个单元测试,它删除数据库中给定类型的所有实体。这应该会导致一堆空表和其他表中的大量零。显然,在空数据库上运行它是没有用的,因此如果发现数据库为空,测试将插入 10.000 个实体。但是,如果它在多线程测试之后运行,它将找到 250.000 个实体,并且删除所有实体需要时间。抱歉,VS2010 不允许我做任何事情。

有趣的是,由于这种情况,我的单元测试开始慢慢变成大型测试,每个测试都需要 30 分钟以上才能完成,然后 VS2010 会让它们超时,因为默认的测试超时是 30 分钟。天哪,请帮忙! :-)

In theory it is correct that tests should be independent of each other and be able to run standalone. But in practice, there is a difference between theory and practice, and VS2010 gives me a hard time with its fixed order of execution (random order that is always the same).

Here are some examples:
I have a unit test that cross checks the dates between some tables and verifies that everything is in agreement. Obviously it is of no use to run this test on an empty database, so I want to to run SOME TIME AFTER the unit test that inserts data. Sorry VS2010 doesn't let you do this.
OK, cool, then I will add it to the insert unit test as an epilogue. But then I want to cross check other 10 things and instead of having a unit test ("Make sure that entities with various parameters can be inserted without crashes") I end up having a mega-test.

Then another case.
My unit test inserts entities, just insert, to make sure that this part of the logic works ok. Then I have a multi-threaded version of the test, to make sure that there are no deadlocks and stuff. Clearly I need the multi-threaded test to run SOME TIME AFTER the single threaded test, and ONLY if the single threaded test succeeds. Sorry, VS2010 can't do this.

Another case. I have a unit test that deletes ALL entities of a given kind in the database. This should result in a bunch of empty tables and lots of zeros in other tables. Clearly it is useless to run it on an empty database, so the test inserts 10.000 entities if it finds the DB empty. However, if it runs AFTER the multithreaded test, it will find 250.000 entities, and to delete ALL of them takes TIME. Sorry, VS2010 won't let me do anything about it.

The funny thing is that because of this situation my unit tests started slowly turning into mega-tests, that took more than 30 mins to complete (each) and then VS2010 would time them out, cause the default test timeout is 30 mins. OMG please help! :-)

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