问题
I am using selenium webdriver in visual studio Using NUNIT C#. The code of test case is
public class MainProjectFile
{
private IWebDriver WDriver;
private log4net.ILog Testlog;
[TestInitialize]
public void wd()
{
WDriver = Helper.GetWebDriver(helperconst.browserType.Chrome);
Testlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
[Priority(1)]
[TestMethod]
public void search()
{
Testlog.Info("Test ID:001, This test will select the criteria and update the customer dropdown");
Testlog.Info("Step 1/1 : Select customer from cutomer dropdown");
var dc = gmethods.GetSelectElement(WDriver, dpo.customermenu);
dc.SelectByText(dpc.customer);
}
[TestCleanup]
public void CleanUp()
{
WDriver.Close();
WDriver.Quit();
}
}
In dpo page:
public static class dpo
{
public const string customermenu = "[data]";
}
In dpc page
public static class dpc
{
public const string customer = "A";
}
In gmethods page:
static public SelectElement GetSelectElement(IWebDriver drv, string elem)
{
SelectElement a = new SelectElement(drv.FindElement(By.CssSelector(elem)));
return a;
}
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?
回答1:
NUnit
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();
}
MSTest
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()
{
}
回答2:
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) { .... }
来源:https://stackoverflow.com/questions/60773269/how-can-i-save-test-case-result-in-a-variable-whether-it-is-passed-or-not-after