简体   繁体   English

单元测试和隔离

[英]Unit Testing and Isolation

I'm aware that unit tests should be completely independent of one another, meaning that the order of operations should never be important. 我知道单元测试应该完全相互独立,这意味着操作的顺序永远都不重要。

How do you handle a scenario where you have test that loads a large document over the network, yet needs other tests to verify information on said document? 您如何处理这样的场景:您的测试通过网络加载了一个大文档,但又需要其他测试来验证该文档上的信息?

I do not want the document requested for each test that has to check this or that. 我不希望每次检查都需要检查的文档。

It works fine if the document is stored as a static member of the test class, and the initial test that fetches it runs first. 如果将文档存储为测试类的静态成员, 并且提取该文档的初始测试将首先运行,则它会很好地工作。 Then the subsequent tests can all do their own thing with the cached copy. 然后,后续测试都可以使用缓存的副本来做自己的事情。

But, again, order of operations with unit tests is wrong, as they should be independent. 但是,再次,单元测试的操作顺序是错误的,因为它们应该是独立的。

What is the best practice in this case? 在这种情况下,最佳做法是什么? I'd prefer to have them all as separate tests and not rolled into one single, big test that can assert all sorts of things about the document after it's been fetched. 我宁愿将它们全部作为单独的测试,而不要合并到一个大的测试中,该测试可以在获取文档后断言有关文档的各种情况。

My current scenario is Visual Studio 2015 Update 3 and C#, but I guess it can be applied to any similar setup. 我当前的场景是Visual Studio 2015 Update 3和C#,但是我想它可以应用于任何类似的设置。

In Visual Studio's test runner, you can set up "playlists" that will let you run them in an order, but it still feels wrong. 在Visual Studio的测试运行器中,您可以设置“播放列表”,以按顺序运行它们,但是仍然感觉很不对劲。

Edit : I'm leaning towards the static approach, in which case each test has to get the document via a single method. 编辑 :我倾向于静态方法,在这种情况下,每个测试都必须通过一种方法来获取文档。 I guess a singleton-type approach (if null, go and get it, otherwise return it). 我猜想是单例类型的方法(如果为null,请获取它,否则返回它)。

But any feedback is good. 但是任何反馈都是好的。

The best option is don't fetch the document over the network, bundle it as part of the test payload. 最好的选择是不要通过网络获取文档, 而是将其作为测试有效内容的一部分进行捆绑。

That being said, if you need one time logic that happens at startup you can add a [AssemblyInitialize] to a test that will run once when the test assembly is loaded. 就是说,如果您需要在启动时发生一次逻辑,则可以向测试添加[AssemblyInitialize] ,该测试在加载测试程序集时将运行一次。 You can do your test configuration there. 您可以在那里进行测试配置。

[TestClass]
public class GlobalTestData
{
    private static byte[] _document;

    public static byte[] Document
    {
        get
        {
            return (byte[])_document.Clone();
        }
    }

    [AssemblyInitialize()]
    public static void DownloadDocument(TestContext context)
    {
        _document = DownloadDocument();
    }

    private static byte[] DownloadDocument()
    {
        //...
    }
}

Just throw that above class in to your project then any test can use GlobalTestData.Document and will get a fresh unaltered copy of the byte[]. 只需将上面的类放入您的项目中,然后任何测试都可以使用GlobalTestData.Document并且将获得byte []的新的不变副本。

Unit Tests test things in isolation. 单元测试是隔离测试的东西。 If you fetch your document over a network connection, it's not isolated. 如果您通过网络连接来获取文档,那么它不是孤立的。 Try to mock the serving of the document. 尝试模拟文件的投放。

You could also fetch the document once in a [TestInitialize] method as suggested by Brian in the comments or in a [ClassInitialize] static method and store it in a local variable, the difference being that TestInitialize runs before every test and ClassInitialize only before the first test is run. 您也可以按照Brian在注释中建议的[TestInitialize]方法或[ClassInitialize]静态方法中的内容提取一次文档,并将其存储在局部变量中,不同之处在于TestInitialize在每个测试之前运行,而ClassInitialize仅在每次测试之前运行。第一次测试运行。

UPDATE: 更新:

If you need the proper document for your tests, you're looking at integration tests. 如果您需要用于测试的正确文档,则可以查看集成测试。 In my opinion, integration tests are perfectly fine to be dependent on one another as they check for correct behaviour if multiple systems have to work together. 我认为,集成测试可以很好地相互依赖,因为如果多个系统必须协同工作,它们可以检查行为是否正确。 Just make sure they're not in the same class as your unit tests, as these should be fast and independent to run. 只要确保它们与您的单元测试不在同一个类中,因为它们应该快速且独立地运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM