I'm trying to setup my tests using Xunit. I have a requirement to delete all images in a folder start of the tests, and then each method does some image resizing and saves a copy of it's output to the folder. The folder should only be emptied once, and then each method will save their own image into the folder.
When I use IUseFixture<T>
, the ClearVisualTestResultFolder
function is still being called before every test, so I only end up with one image in the folder.
public class Fixture { public void Setup() { ImageHelperTest.ClearVisualTestResultFolder(); } } public class ImageHelperTest : IUseFixture<EngDev.Test.Fixture> { public void SetFixture(EngDev.Test.Fixture data) { data.Setup(); } public static void ClearVisualTestResultFolder() { // Logic to clear folder } }
If I put the ClearVisualTestResultFolder
in the constructor, it is also being called once for every test method. I need this just run once before all test methods are executed, how can I achieve this?
If it matters, I use the ReSharper test runner.
xUnit.net offers several methods for sharing this setup and cleanup code, depending on the scope of things to be shared, as well as the expense associated with the setup and cleanup code.
Instead, the class constructor is used for test initialization and the Dispose method along with deriving from IDisposable indicates that there is test cleanup code.
xUnit.net does not require an attribute for a test class; it looks for all test methods in all public (exported) classes in the assembly. Set the Skip parameter on the [Fact] attribute to temporarily skip a test.
Running unit tests in parallel is a new feature in xUnit.net version 2. There are two essential motivations that drove us to not only enable parallelization, but also for it to be a feature that's enabled by default: As unit testing has become more prevalent, so too have the number of unit tests.
Following the guidance in this xUnit discussion topic, it looks like you need to implement a constructor on the Fixture and also implement IDisposable. Here's a complete sample that behaves the way you want:
using System; using System.Diagnostics; using Xunit; using Xunit.Sdk; namespace xUnitSample { public class SomeFixture : IDisposable { public SomeFixture() { Console.WriteLine("SomeFixture ctor: This should only be run once"); } public void SomeMethod() { Console.WriteLine("SomeFixture::SomeMethod()"); } public void Dispose() { Console.WriteLine("SomeFixture: Disposing SomeFixture"); } } public class TestSample : IUseFixture<SomeFixture>, IDisposable { public void SetFixture(SomeFixture data) { Console.WriteLine("TestSample::SetFixture(): Calling SomeMethod"); data.SomeMethod(); } public TestSample() { Console.WriteLine("This should be run once before every test " + DateTime.Now.Ticks); } [Fact] public void Test1() { Console.WriteLine("This is test one."); } [Fact] public void Test2() { Console.WriteLine("This is test two."); } public void Dispose() { Console.WriteLine("Disposing"); } } }
When running this from the console runner, you'll see the following output:
D:\xUnit>xunit.console.clr4.exe test.dll /html foo.htm xUnit.net console test runner (64-bit .NET 4.0.30319.17929) Copyright (C) 2007-11 Microsoft Corporation.
xunit.dll: Version 1.9.1.1600 Test assembly: test.dll
SomeFixture ctor: This should only be run once
Tests complete: 2 of 2
SomeFixture: Disposing SomeFixture
2 total, 0 failed, 0 skipped, took 0.686 seconds
Then, when you crack open the test output file foo.htm, you'll see the other test output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With