我在Visual Studio 2008裏面使用了MSTEST。我如何讓每個單元測試方法在某個測試類中運行,就好像它是第一次運行測試一樣,以便在運行每個測試之前重置所有全局狀態?我不想使用TestInitialize,ClassInitialize,AssemblyInitialize等。例如明確清理世界:如何在運行之前強制MSTEST TestMethod重置所有單例/靜態?
[TestClass]
public class MyClassTests
{
[TestMethod]
public void Test1()
{
// The "Instance" property creates a new instance of "SomeSingleton"
// if it hasn't been created before.
var i1 = SomeSingleton.Instance;
...
}
[TestMethod]
public void Test2()
{
// When I select "Test1" and "Test2" to run, I'd like Test2
// to have a new AppDomain feel so that the static variable inside
// of "SomeSingleton" is reset (it was previously set in Test1) on
// the call to ".Instance"
var i2 = SomeSingleton.Instance;
// some code
}
雖然similar question上出現了這個話題,只澄清,測試不平行運行。我意識到測試是連續運行的,但似乎沒有辦法爲每個方法明確強制一個新的AppDomain(或者相當於清除所有狀態的東西)。
理想情況下,我想爲我的單元測試的一小部分指定此行爲,這樣我就不必爲不關心全局狀態的測試支付新的AppDomain創建懲罰(絕大多數我的測試)。
你有代碼示例Jeff嗎? – DevDave 2012-04-30 10:49:43
是的,一個代碼示例請 – Harindaka 2013-02-03 09:47:39