Visual Studio를 사용하면 테스트 단위 클래스를 생성 할 수 있습니다. 그런 다음 클래스 초기화 메소드에 주석을 달아 라. 그 안에는 testContext 인수를 사용하여 속성을 추가하십시오.TestContext.Properties를 사용할 수 있습니까?
테스트 앱을 시작할 때이 방법은 실제로 테스트 인프라에서 호출됩니다.
//Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
/*
* Any user defined testContext.Properties
* added here will be erased after this method exits
*/
testContext.Properties.Add("key", 1) ; // place the break point here
}
MyClassInitialize에서 나가면 사용자가 추가 한 속성이 모두 손실됩니다. 단지 10 개의 "공식적인"것들만 남아 있습니다.
실제로 각 테스트 메소드가 호출되기 전에 TestContext는 inital 공식 스크립트로 덮어 쓰여집니다. 사용자가 테스트 초기화 메소드를 가지고있는 경우에만 덮어 쓰지 않으며, 거기서 변경 한 사항은 테스트에 전달됩니다.
//Use TestInitialize to run code before running each test
[TestInitialize()]public void MyTestInitialize(){
this.TestContext.Properties.Add("this is preserved",1) ;
}
이것은 실제로 사용자를 위해 TestContext.Properties가 "주로"읽기 전용임을 의미합니다. MSDN에 명확히 설명되어 있지 않습니다.
이것은 매우 지저분한 디자인 + 구현입니다. TestContext.Properties를 콜렉션으로 사용하는 이유는 무엇입니까? 사용자는 클래스 와이드 초기화를 위해 다른 많은 솔루션을 수행 할 수 있습니다.
필자는 실제로 그것을 사용하는 테스트 컨텍스트의 목적을 이해하지 못했습니다. –
https://blogs.msdn.microsoft.com/vstsqualitytools/2006/01/09/using-testcontext-in-unit-tests/ –