Are TestContext.Properties usable ?

Posted by DBJDBJ on Stack Overflow See other posts from Stack Overflow or by DBJDBJ
Published on 2010-03-25T16:36:43Z Indexed on 2010/03/27 9:13 UTC
Read the original article Hit count: 194

Filed under:
|
|

Using Visual Studio generate Test Unit class. Then comment in, the class initialization method. Inside it add your property, using the testContext argument.

Upon test app startup this method is indeed called by the testing infrastructure.

//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
}

After leaving MyClassInitialize, any properties added by user are lost. Only the 10 "official" ones are left.

Actually TestContext gets overwritten, with the inital offical one, each time before each test method is called. It it not overwritten only if user has test initialization method, the changes made over there are passed to the test.

//Use TestInitialize to run code before running each test
[TestInitialize()]public void MyTestInitialize(){ 
     this.TestContext.Properties.Add("this is preserved",1) ;
}

This effectively means TestContext.Properties is "mostly" read only, for users. Which is not clearly documented in MSDN.

It seems to me this is very messy design+implementation. Why having TestContext.Properties as an collection, at all ? Users can do many other solutions to have class wide initialization.

Please discuss.

--DBJ


© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing