C#无法使用Test Attribute获取if条件中的数据

时间:2016-03-02 21:53:57

标签: c# nunit

我试图通过[Teardown]属性以txt文件格式添加一些文本,它会检查testcase是通过还是失败,因此它会将数据添加到文本文件中。 if-else条件检测到pass或fail。其他条件(通过测试)工作正常。但是当我想要添加失败测试时,它不会给出预期的结果

对于通过测试 - 结果

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. 总时间:
  5. 以下是我的代码:

    [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

2 个答案:

答案 0 :(得分:0)

如果删除评论并且调用抛出异常,则永远不会执行以下行,因此未设置值。

答案 1 :(得分:0)

[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);
    }   
  }

Try doing this, this might help you. Charlie is right but I think if you do this will solve your problem.

When NUnit catches an error it comes out of the method from the point where it broke. So the way I did will help to get this data as well. There are three changes and I have marked with this <-------