Test Context

The TestContext is the class allows tests to access certain information about the execution context.

To get the information of the Test such as Method name, Class Name, Properties of the Test, Result of the Test , Nunit provides TestContext class.

In the automated test, the Test information will be useful,

Example :        

- If the test is failed the screenshot can be taken.
- If the test is failed then the next action can be performed.
- To get the clear reporting Test name / Class name will be helpful.

 

string name = TestContext.CurrentContext.Test.MethodName;
Console.WriteLine("Name of the method - " + name);
name = TestContext.CurrentContext.Test.ClassName;
Console.WriteLine("Name of the classname - " + name);

 

Test status,stackrace,properties are preferred in Tear down method

name = TestContext.CurrentContext.Result.Outcome.Status.ToString();
Console.WriteLine("Name of the status - " + name);

 

Test stackrace are preferred in Tear down method

name = TestContext.CurrentContext.Result.StackTrace;
Console.WriteLine("Stackrace - " + name);
name = TestContext.CurrentContext.Test.Properties.Get("Category").ToString(); // If the category name is given in the test
Console.WriteLine("Category name - " + name);

 

 

Related Tutorials