TestContext – more on mstest

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 we need add the following properties in our Test Class –

private TestContext testContextInstance;

///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext {
 get
 {
 return testContextInstance;
 }
 set
 {
 testContextInstance = value;
 }
}

The property is automatically provided with a concrete instance of the TestContext type [1]. In essence, it implies that we do not need to instantiate or derive TestContext in our test class.

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, 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.

More details on TestContext is available here.

References
[1] http://msdn.microsoft.com/en-us/library/ms404699

Advertisements

One thought on “TestContext – more on mstest

  1. Plz tell me is it possible to do unit testing on form events. and how we can test function that returns dataset from the database?
    plz reply me asap.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s