尼尼特。使用 ITestListener 截取测试失败的屏幕截图
Nunit. Take screenshot on test failure using ITestListener
我想对失败的测试用例进行截图。但我不知道如何强制 Nunit 使用我的监听器。
我试图使用 IAddins,但 Nunit 没有 NUnit.Core.Extensibility 库。
我的代码:
using System;
using OpenQA.Selenium;
using NUnit.Framework.Interfaces;
using AT_MentoringPortal.Driver;
using System.Drawing.Imaging;
namespace AT_MentoringPortal.listeners
{
public class ScreenshotListener : ITestListener
{
private readonly string path = ".//screens//";
public void TestFinished(ITestResult result)
{
if (result.ResultState.Status == TestStatus.Failed)
{
IWebDriver driver = DriverFactory.GetDriver();
this.MakeScreenshot(driver, result.Name);
}
}
public void MakeScreenshot(IWebDriver driver, string testName)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd-hhmm-ss");
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile($"{this.path}{timestamp} {testName}", ImageFormat.Jpeg);
}
public void TestOutput(TestOutput output)
{
// throw new NotImplementedException();
}
public void TestStarted(ITest test)
{
// throw new NotImplementedException();
}
}
}
请告诉我如何在测试中启动我的侦听器 class。
ITestListener 是 NUnit 在 运行 测试中使用的内部接口。 NUnit V2 (TestListener) 中有一个类似的接口,您可以创建使用它的插件。 NUnit 3 没有 NUnit 2 那样的插件,尽管它可以通过其他方式扩展。
您是否只想为某些测试保存屏幕截图?或者针对特定夹具中的每个测试?还是更笼统?
要在夹具中执行此操作,您可以使用 OneTimeTearDown 方法。
我想对失败的测试用例进行截图。但我不知道如何强制 Nunit 使用我的监听器。 我试图使用 IAddins,但 Nunit 没有 NUnit.Core.Extensibility 库。 我的代码:
using System;
using OpenQA.Selenium;
using NUnit.Framework.Interfaces;
using AT_MentoringPortal.Driver;
using System.Drawing.Imaging;
namespace AT_MentoringPortal.listeners
{
public class ScreenshotListener : ITestListener
{
private readonly string path = ".//screens//";
public void TestFinished(ITestResult result)
{
if (result.ResultState.Status == TestStatus.Failed)
{
IWebDriver driver = DriverFactory.GetDriver();
this.MakeScreenshot(driver, result.Name);
}
}
public void MakeScreenshot(IWebDriver driver, string testName)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd-hhmm-ss");
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile($"{this.path}{timestamp} {testName}", ImageFormat.Jpeg);
}
public void TestOutput(TestOutput output)
{
// throw new NotImplementedException();
}
public void TestStarted(ITest test)
{
// throw new NotImplementedException();
}
}
}
请告诉我如何在测试中启动我的侦听器 class。
ITestListener 是 NUnit 在 运行 测试中使用的内部接口。 NUnit V2 (TestListener) 中有一个类似的接口,您可以创建使用它的插件。 NUnit 3 没有 NUnit 2 那样的插件,尽管它可以通过其他方式扩展。
您是否只想为某些测试保存屏幕截图?或者针对特定夹具中的每个测试?还是更笼统?
要在夹具中执行此操作,您可以使用 OneTimeTearDown 方法。