在解决方案中运行所有测试时的单元测试序列 [英] Unit test sequence when running all tests in solution

查看:29
本文介绍了在解决方案中运行所有测试时的单元测试序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个复杂的 VS2008 解决方案中,我有三个单元测试项目.由于它们在同一个测试数据库上运行,因此一个接一个地执行测试项目很重要.首先哪个项目并不重要,重要的是一个在另一个开始之前完成.

In a complex VS2008 solution I have three unit test projects. As they operate on the same test database it is important that the test projects are executed one after the other. It is not important which project first, just that one is finished before the other starts.

如果我想全部执行它们,有几种方法可以做到,这会导致不同的结果:

If I want to execute them all, there are several ways to do that, which lead to different results:

  • 我有一个测试列表 .vsmdi 文件,其中测试按项目排序.如果我打开列表并从测试列表编辑器执行测试,一切都很好.
  • 如果我打开测试视图"窗口,按项目对测试进行排序并运行它们,再次一切正常.
  • 但是,如果我通过从菜单中选择测试 -> 运行 -> 解决方案中的所有测试"来运行测试,它们会以随机顺序执行,其中一些失败,因为其他测试项目之一已经操纵了测试分贝.

那么问题来了,当使用第三种方法时,是什么决定了单元测试的顺序?有没有办法在 .testrunco​​nfig 中指定默认测试列表?

So the question is, what determines the unit test sequence when using the third approach? Is there a way to specify a default test list in the .testrunconfig?

由于有解决方法,所以问题根本不重要.但任何想法都是受欢迎的.谢谢.

As there are workarounds, the issue is not critical at all. But any thoughts are welcome. Thanks.

推荐答案

您正在体验一种称为交互式测试的单元测试味道,在优秀的xUnit 测试模式.更具体地说,您受到共享夹具的影响,这是另一种说法,您的多个测试使用相同的共享数据库,并且它们依赖于先前测试运行的结果.

You are experiencing a unit testing smell called Interactive Tests, described in the excellent xUnit Test Patterns. More specifically, you suffer from a Shared Fixture, which is another way of saying that several of your tests use the same shared database, and that they are dependent on the outcome of previous test runs.

这是一种反模式,应该不惜一切代价避免.这本书提供了有关如何处理这种情况的广泛指导.

This is an anti-pattern and should be avoided at all cost. The book offers extensive guidance on how to deal with this situation.

我开始描述这一点的原因是,这就是 MSTest 不保证测试顺序的原因.尽管顺序可能看起来(半)确定性,但您不能依赖它.其他一些单元测试框架(想到 xUnit.NET)甚至会在每次运行测试套件时以随机顺序运行测试.MSTest 并不是那么极端,但您无法对单元测试进行排序.

The reason I start out by describing this is that this is the reason that MSTest doesn't guarantee ordering of the tests. Although the order may seem (semi)deterministic, you can't rely on it. Some other unit testing frameworks (xUnit.NET comes to mind) even go so far as to run tests in a random order each time you run the test suite. MSTest isn't that extreme, but there are no way you can order your unit tests.

也就是说,如果您正在运行 Visual Studio Team Suite 或(我相信)Team Test,则有一种称为 Ordered Test 的测试.您可以使用它来指定所有测试(包括单元测试)应按特定顺序执行.

That said, if you are running Visual Studio Team Suite or (I believe) Team Test, there's a type of test called an Ordered Test. You can use that to specify that all tests (including unit tests) should execute in a specific order.

这篇关于在解决方案中运行所有测试时的单元测试序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆