2016-10-26 28 views
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(); 
     } 
    } 
} 

請告訴我如何開始我的聽衆在測試類。

+0

您使用的是什麼版本的NUnit? – Charlie

+0

@Charlie我使用的版本3.5.0(最新)Nunit和Nunit3TestAdapter。 –

回答

0

ITestListener是NUnit自己在運行測試時使用的內部接口。 NUnit V2中有一個類似的接口(TestListener),您可以創建使用它的插件。 NUnit 3沒有像NUnit 2那樣的插件,儘管它可以用其他方式擴展。

難道要保存的屏幕截圖僅用於某些測試?或者對於某個夾具中的每個測試?或者更普遍?

於燈具內做到這一點,你可以使用一個OneTimeTearDown方法。

相關問題