Last active
September 25, 2016 09:41
-
-
Save Sebazzz/5c04b92f566bc8e9c6397b2f10d4f191 to your computer and use it in GitHub Desktop.
ITestAction placed on assembly level does not run before and after each test fixture. Place the nunit 3 assemblies in the directory of this gist, and run 'run.bat'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
csc Test.cs /target:library /debug+ /debug:full /reference:nunit.framework.dll | |
nunit3-console.exe Test.dll |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: TestActions.MyTestAction] | |
namespace TestActions { | |
using System; | |
using System.IO; | |
using System.Threading; | |
using NUnit.Framework; | |
using NUnit.Framework.Interfaces; | |
[AttributeUsage(AttributeTargets.Assembly)] | |
public class MyTestActionAttribute : Attribute, ITestAction { | |
public void BeforeTest(ITest test) { | |
TestLog.Instance.Log("Before: " + test.FullName); | |
} | |
public void AfterTest(ITest test) { | |
TestLog.Instance.Log("After: " + test.FullName); | |
} | |
public ActionTargets Targets => ActionTargets.Suite | ActionTargets.Test; | |
} | |
public sealed class TestLog : IDisposable { | |
private static readonly Lazy<TestLog> LazyInstance = new Lazy<TestLog>(() => new TestLog(), LazyThreadSafetyMode.ExecutionAndPublication); | |
public static TestLog Instance => LazyInstance.Value; | |
private readonly StreamWriter _log; | |
private TestLog() { | |
this._log = new StreamWriter(Path.Combine(Path.GetDirectoryName(typeof(TestLog).Assembly.Location), "TestLog.txt"), false); | |
AppDomain.CurrentDomain.DomainUnload += (_, __) => this.Dispose(); | |
} | |
public void Log(string msg) { | |
this._log.WriteLine(msg); | |
this._log.Flush(); | |
} | |
public void Dispose() { | |
this._log.Flush(); | |
this._log.Close(); | |
} | |
} | |
[TestFixture] | |
public sealed class MyTestFixture { | |
[Test] | |
public void Test1() { | |
TestLog.Instance.Log("MyTestFixture.Test1"); | |
} | |
[Test] | |
public void Test2() { | |
TestLog.Instance.Log("MyTestFixture.Test2"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment