【问题标题】:Why is UnitTestOutcome set to Unknown when running tests on TeamCity?为什么在 TeamCity 上运行测试时 UnitTestOutcome 设置为未知?
【发布时间】:2013-12-10 14:32:28
【问题描述】:

我在我的TestCleanup 方法中检查TestContext.CurrentTestOutcome,以便在测试未通过时执行操作(在这种情况下,测试使用 Selenium 来运行一个网站,如果测试我正在保存屏幕截图不通过)。

private static TestContext _testContext;

private static IWebDriver _driver;

[ClassInitialize]
public static void SetupTests(TestContext testContext)
{
    _testContext = testContext;
    _driver = new FirefoxDriver();
}

[TestCleanup]
public void TeardownTest()
{
    if (_testContext.CurrentTestOutcome != UnitTestOutcome.Passed)
    {
        var fileName = Path.Combine(
            Environment.CurrentDirectory,
            string.Format("{0}.{1}.gif", _testContext.FullyQualifiedTestClassName, _testContext.TestName));

        ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Gif);

        Console.WriteLine("Test outcome was {0}, saved image of page to '{1}'", _testContext.CurrentTestOutcome, fileName);
    }
}

这在使用 ReSharper 的本地开发 PC 上运行时效果很好,但在我们的构建服务器(使用 TeamCity)上,UnitTestOutcome 始终为 Unknown,尽管 TeamCity 报告它们已通过。

documentation on MSDN 不是很有帮助。什么会导致此值设置为 Unknown

【问题讨论】:

  • 在本地运行时,测试如何运行?锐器? Visual Studio 的内置 MSTest 工具?
  • @Arran - 我们正在使用 Resharper 在本地运行测试。我已经相应地更新了我的问题。

标签: c# selenium-webdriver mstest teamcity-8.0


【解决方案1】:

根据http://confluence.jetbrains.com/display/TCD8/MSTest+SupportTeamCity 不支持即时报告单个测试结果,它会解析测试结果文件以将结果提供给构建步骤。

这将解释 TeamCity 如何能够将测试报告为已通过,即使在单个测试完成时 UnitTestOutcome 可能是未知的。

上面的链接提到“MSTest 工具的细节”是非即时测试结果报告的原因,所以我只能推测相同的细节可能意味着从构建服务器运行时 TestContext 不可用。

另外,TestContext.CurrentTestOutcome 的 MSDN 文档确实提到需要对直接调用者的完全信任。 TeamCity 可能以仅部分受信任的方式执行测试,因此导致测试结果未知。

检查 MSTest 是否是您的问题的一种快速方法是使用以下命令切换到 NUnit:

#if NUNIT
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using IgnoreAttribute = NUnit.Framework.IgnoreAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IgnoreAttribute = Microsoft.VisualStudio.TestTools.UnitTesting.IgnoreAttribute;
#endif

不过,您必须在 TeardownTest 方法中执行类似的操作才能使用 NUnit TestContext.CurrentContext.Result.Status

【讨论】:

    【解决方案2】:

    解决此问题的方法是使用TestContext 的公共属性,而不是使用传递给[ClassInitialize] 方法的参数。

    public TestContext TestContext { get; set; }
    

    测试运行者将automatically set the property

    (这与我posted on SO的另一个问题有关)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多