【发布时间】:2016-03-05 02:04:33
【问题描述】:
我正在尝试通过 [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
从失败的测试结果来看,我没有得到以下结果
- 测试用例编号
- 测试详情:
- 结束时间:
- 总时间:
下面是我的代码:
[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
【问题讨论】:
-
您使用的是 NUnit 2.6 还是 3.0?
-
是的,我使用的是 NUnit 2.6.4