[英]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.