简体   繁体   English

如何使用单个变量来跟踪所有测试

[英]How can I use a single variables to track all of my tests

I'm using MSTest, when I first run all of my unit tests (or tests on their own) i want to create a unique identifier that I can put into db records to track the test. 我正在使用MSTest,当我第一次运行所有的单元测试(或单独进行测试)时,我想创建一个唯一的标识符,可以将其放入db记录中以跟踪测试。 Problem is I want the same unique reference to be created and used across all tests. 问题是我希望在所有测试中创建并使用相同的唯一引用。 What I really want to use is a DateTime stamp. 我真正想要使用的是DateTime戳。 I'm looking for an event that always gets raised and then I can put it in a static container for the duration of the tests and then just access this static container from within the tests... Is this possible?.... 我正在寻找一个总是引发的事件,然后可以在测试期间将其放入静态容器中,然后仅在测试中访问该静态容器即可。这可能吗?

You could go down the route of having a separate class responsible for holding a static DateTime : 您可以走一个单独的类负责保存静态DateTime的路线:

public static class TestIdGenerator
{
    private static readonly Lazy<DateTime> _testId = new Lazy<DateTime>(() => DateTime.Now);
    public static DateTime TestId
    {
        get { return _testId.Value; }
    }
}

in your test, you access it with 在测试中,您可以使用

var testId = TestIdGenerator.TestId;

The DateTime will be set the first time the TestId property is accessed, and will remain the same on each subsequent access until the CLR is unloaded - which will happen when all the tests in a particular test run have completed. DateTime将在首次访问TestId属性时进行设置,并且在每次后续访问之前都将保持不变,直到CLR卸载为止-当特定测试运行中的所有测试都完成时,将发生这种情况。

I've just tested this, and it does remain constant for all the tests in the fixture, but is then different on the next test run. 我刚刚进行了测试,并且在固定装置中的所有测试中它都保持不变,但是在下一次测试中将有所不同。

You could use the AssemblyInitialize attribute on a method to ensure it runs before any other methods in your test assembly. 您可以在方法上使用AssemblyInitialize属性,以确保它在测试程序集中的任何其他方法之前运行。 In that method you could generate your unique ID and set it to a static variable. 在这种方法中,您可以生成唯一的ID并将其设置为静态变量。 If your testing methods span assemblies this won't work though. 如果您的测试方法跨程序集,则将无法使用。

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

相关问题 Unity C#-如何做到这一点,以便可以通过一个类访问所有其他类,变量和方法? - Unity c# - How can I make it so that I can access all my other classes, variables and methods via a single class? 如何在我的 blackSnowEvent function 中将变量用作 arguments? - How can I use variables as arguments in my blackSnowEvent function? 如何在单元测试中使用 Mock 对象并仍然使用代码覆盖率? - How can I use Mock Objects in my unit tests and still use Code Coverage? 如何按喜好对测试进行排序? - How can i sort tests by my liking? 我怎样才能对我的所有财产使用“不同” - how can I use “distinct” with all my property 如何在没有全局变量的情况下始终保留变量? - How can I keep my variable all the time without global variables? 如何在c#项目中找到所有静态变量? - How can I find all static variables in my c# project? 如何在C#中发送针对我所有应用程序的单个消息? - How can I send a single message targeted to all my applications in C#? 如何获取构造函数中所有变量的列表或变量值? - How can i get a list of all variables in the constructor or the variables values? 如何在服务器广播中使用 signalr 仅对单个客户端,而不是所有客户端? - How can I use signalr in server broadcast to a single client only, not all clients?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM