I am using selenium webdriver in visual studio Using NUNIT. The code of test case is
I want to Log that test case is passed or fail in a variable right after the test case is executed.How can I achieve that?
I am using selenium webdriver in visual studio Using NUNIT. The code of test case is
I want to Log that test case is passed or fail in a variable right after the test case is executed.How can I achieve that?
Assume you use NUnit (because what's in the question looks like MSTest), you could write custom IWrapTestMethod
attribute to extend the test command. The attribute gives you an opportunity to inspect on the test and test result before and after each test run:
// I don't know where your variable is, so just use this dummy class with
// some dummy static properties.
class TestResultKeeper
{
public static TestResult TestResult { get; set; }
public static ResultState ResultState => TestResult?.ResultState ?? ResultState.Failure;
public static bool IsPassed => ResultState == ResultState.Success;
public static Exception Exception { get; set; }
}
// Custom NUnit 3 attribute to extend test method
public class LogTestAttribute : Attribute, IWrapTestMethod
{
public TestCommand Wrap(TestCommand command)
{
// Wrap test command
return new LogTestResultCommand(command);
}
}
class LogTestResultCommand : DelegatingTestCommand
{
public LogTestResultCommand(TestCommand innerCommand)
: base(innerCommand)
{
}
public override TestResult Execute(TestExecutionContext context)
{
try
{
var result = innerCommand.Execute(context);
// Set test result to TestResultKeeper
TestResultKeeper.TestResult = result;
TestResultKeeper.Exception = null;
return result;
}
catch (Exception e)
{
// Exception thrown from test. Test failed.
TestResultKeeper.TestResult = null;
TestResultKeeper.Exception = e.InnerException;
throw;
}
}
}
Usage:
[Test, LogTest]
public void Test1()
{
Assert.Fail();
}
[Test, LogTest]
public void Test2()
{
Assert.Pass();
}
Assume your test is written in MSTest (as per your example), you could create a new test method attribute and derive from TestMethodAttribute
, and do similar things as above:
class TestResultKeeper
{
public static TestResult TestResult { get; set; }
public static UnitTestOutcome TestOutcome => TestResult?.Outcome ?? UnitTestOutcome.Failed;
public static bool IsPassed => TestOutcome == UnitTestOutcome.Passed;
public static Exception Exception { get; set; }
}
public class LogTestTestMethodAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
var testResult = base.Execute(testMethod)[0];
TestResultKeeper.TestResult = testResult;
TestResultKeeper.Exception = testResult.TestFailureException;
return new[] { testResult };
}
}
Usage:
[LogTestTestMethod]
public void TestMethod1()
{
}
For NUnit you can access the result and other details of the test using properties found in TestContext.CurrentContext.
For your problem you can add the following check to the test teardown method
if(TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed) { .... }
Edit to include MSTest:
For MSTest add the following property to your test class
public TestContext TestContext { get; set; }
Then use it by adding the following to TestCleanup
if(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed) { .... }