C#无法在具有测试属性的条件下获取数据

C# Unable to get data in if condition with Test Attribute

我正在尝试通过 [Teardown] 属性添加一些 txt 文件格式的文本,它检查测试用例是通过还是失败,并相应地将数据添加到文本文件中。通过或失败由 if-else 条件检测。其他条件(通过测试)工作正常。但是当我试图添加失败测试时,它没有给出预期的结果

通过测试 - 结果

Test Case No.: 12345
Test Details: For test purpose
Start Time: 3/2/2016 3:51:28 PM
End Time: 3/2/2016 3:51:31 PM
Total Time: 0 minutes and 2 seconds
Status: Passed

对于失败的测试 - 结果

Test Case No.: 
Test Details: 
Start Time: 3/2/2016 3:49:37 PM
End Time: 1/1/0001 12:00:00 AM
Total Time: -49 minutes and -37 seconds
Status: Failed

从失败的测试结果来看,我没有得到下面的东西

  1. 测试用例编号
  2. 测试详情:
  3. 结束时间:
  4. 总时间:

下面是我的代码:

[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(ChromeDriver))]
public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new()
{
    static DateTime _timestamp1;
    static DateTime _timestamp2;
    static string _TestNum;
    static string _TestDetails;
[Test]
public void SurfGoogle()
{
    _timestamp1 = DateTime.Now;
    TWebDriver driver = new TWebDriver();
   driver.Navigate().GoToUrl("http://www.google.com");
  //driver.FindElement(By.Id("q"));
   _timestamp2 = DateTime.Now;
   _TestNum = "12345";
   _TestDetails = "For test purpose";
}


[TearDown]
public void teardown()
{

    string path = (@"..\..\Result\");
    TimeSpan timediff = _timestamp2 - _timestamp1;
    string TotalTime = timediff.Minutes + " minutes and " + timediff.Seconds + " seconds";

    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        string status = "Failed";

        File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }
    else
    {
        string status = "Passed";

        File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }   
  }
}

要使测试失败,您可以从我的代码中删除注释部分。

使用 NUnit:2.6.4

如果删除注释并且调用引发异常,则永远不会执行以下行,因此不会设置值。

[Test]
public void SurfGoogle()
{
    _timestamp1 = DateTime.Now;  
    _TestNum = "12345";                  <-------
    _TestDetails = "For test purpose";   <-------

    TWebDriver driver = new TWebDriver();
    driver.Navigate().GoToUrl("http://www.google.com");
    driver.FindElement(By.Id("q"));
  }
}

[TearDown]
public void teardown()
{

    string path = (@"..\..\Result\");
    _timestamp2 = DateTime.Now;          <-------
    TimeSpan timediff = _timestamp2 - _timestamp1;
    string TotalTime = timediff.Minutes + " minutes and " + timediff.Seconds + " seconds";

    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        string status = "Failed";

        File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }
    else
    {
        string status = "Passed";

        File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }   
  }

尝试这样做,这可能对您有所帮助。查理是对的,但我认为如果你这样做会解决你的问题。

当 NUnit 捕获到错误时,它会从方法中断的地方退出。所以我所做的方式也将有助于获得这些数据。一共有三处变化,我用这个做了标记 <-------