1
我想截取失敗的測試用例的截圖。但我不知道如何強迫Nunit使用我的聽衆。 我試圖使用IAddins,但是Nunit沒有NUnit.Core.Extensibility庫。 我的代碼:Nunit。使用ITestListener對測試失敗截圖
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();
}
}
}
請告訴我如何開始我的聽衆在測試類。
您使用的是什麼版本的NUnit? – Charlie
@Charlie我使用的版本3.5.0(最新)Nunit和Nunit3TestAdapter。 –