More on Unit Testing : TestContext

From empirical evidence, we know that improving code coverage by verifying all the boundary conditions, and ensuring quality of code-base significantly enhances maintainability and comprehensibility of complex software systems. Therefore, writing effective unit and integration tests has become one of the integral part in software development life cycle. In fact, development methodology such as TDD has been embraced rapidly due to these realized benefits. In today's post, we talk more about TestContext of mstest. In particular, we focus on its usage in devising effective unit tests. Before moving forward, we would like to outline the previous posts of the series regarding Unit Testing.

What is TestContext?

TestContext is an abstract class of Microsoft.VisualStudio.TestTools.UnitTesting namespace. It exposed several properties related to the current context (i.e., current test run). In addition, it provides following supports: (read more)


  • i think it best article for those who r going to start wrtiting unit testing.

  • Thanks for demonstrating more about how to use TestContext than MSDN examples. This is helpful for me as I'm transitioning from nUnit to MS Test.

  • Thank you very much!

  • Where is your private TestContext object being assigned to? None of the code in the article shows this and so all your tests will fail because TestContext will always be null in your UserManagerTests class.

  • Philip,

    Thanks for your comment. I will update this post with the clarification on how the TestContext gets initialized.

    This is what happens:

    "When you run a unit test, you are automatically provided with a concrete instance of the TestContext type, if the test class that contains your unit test method has a TestContext property defined. This means that you do not have to instantiate or derive the TestContext type in your code"  [1].



Comments have been disabled for this content.