TestContext - more on VSTS unit testing

December 22, 2007

If we are in test driven development, we need to spend significant amount of time in writing unit tests. In VSTS unit testing framework, every test run create a unique folder in our machine and generate a test report ( *.trx format) for every test run-  


testresult.jpg

 

 

Sometimes it’s needed to access the folder that current test run created. UnitTestAdapterContext class provides a set of properties related to current test run that can be accessed from inside unit tests.

 To use the properties provided in UnitTestAdapterContext class we need to – add the following properties in our Test Class –  

 

 

private TestContext testContextInstance;

 /// <summary>

 ///Gets or sets the test context which provides

 ///information about and functionality for the current test run.
      
 ///</summary>

 public TestContext TestContext

        {

            get

            {

                return testContextInstance;

            }

           set

            {

               testContextInstance = value;

            }

       }

 

The property is automatically assigned by the test run engine of VSTS. Take a look at the properties and functionalities offered by TestContext

 

testconttextcd.jpg

 If I start debugging a unit test adding a break point and watch the TestContext, we can view different information about and functionality for current test run -   

unittestadaptercontext.jpg  

 

So ,picece of cake - Current TestRun Directory can be accessed by just one line of code-      

 

string testDirectory = TestContext.TestDir; 

Therefore , we can use class in our TestContext Unit Tests to get different informations about the current test run and we can use different functioanallity of this class – for example – BeginTimer() and EndTimer() would be verify useful to measure our application’s differenent module’s performance to validate certain non-functional requirements. J  

Want to know more about TestContext?  - Click Here