更新时间:2023-02-25 16:46:38
能否将共享数据放入静态变量中?
Can you just place your shared data into static variables?
像这样简单的事情
private static string testValue = "something";
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(testValue, "something");
testValue = "something2";
}
[TestMethod]
public void TestMethod2()
{
Assert.AreEqual(testValue, "something2");
testValue = "something3";
}
[TestMethod]
public void TestMethod3()
{
Assert.AreEqual(testValue, "something3");
}
更新:总结关于此问题的其他评论,我认为每个人都完全同意保持单元测试之间的状态是一个坏主意.我的答案只是执行此操作的一种方法(如有必要).正如我在另一条评论中提到的那样,过去我必须这样做,但对于单元测试则不必.在不一定要模拟对象或隔离应用程序各部分的集成/回归测试之间保持状态有时是有益/必要的.作为软件测试人员,您并不总是具有影响力/能力/权限来立即重构或重新构造应用程序以使其最适合此测试.
Update: To summarize other comments on this question, I think that everyone is in complete agreement that keeping state between unit tests is a bad idea. My answer was simply a way to do this, if necessary. As I mentioned in another comment, I have had to do this in the past, but not for unit tests. It is sometimes beneficial/necessary to keep state between integration/regression tests where you don't necessarily want to mock objects or isolate parts of the application. As a software tester, you do not always have the influence/ability/permission to immediately refactor or rearchitect an application to be optimal for this testing.