Skip to content

Instantly share code, notes, and snippets.

@Sebazzz
Last active September 25, 2016 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebazzz/5c04b92f566bc8e9c6397b2f10d4f191 to your computer and use it in GitHub Desktop.
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'.
@echo off
csc Test.cs /target:library /debug+ /debug:full /reference:nunit.framework.dll
nunit3-console.exe Test.dll
[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