{"id":1011,"date":"2017-02-19T14:02:19","date_gmt":"2017-02-19T14:02:19","guid":{"rendered":"http:\/\/tpodolak.com\/blog\/?p=1011"},"modified":"2017-02-24T22:02:40","modified_gmt":"2017-02-24T22:02:40","slug":"xunit-sharing-test-data-across-assembly","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2017\/02\/19\/xunit-sharing-test-data-across-assembly\/","title":{"rendered":"XUnit &#8211; sharing test data across assembly"},"content":{"rendered":"<h3>1. Introduction<\/h3>\n<p><i>XUnit<\/i> provides two ways of sharing data between tests &#8211; <i>ICollectionFixture<\/i> and <i>IClassFixture<\/i>. The first one allows you to shared context across collections and the second one across test classes. I&#8217;ve already had a couple of cases in which these fixtures were not enough. Basically, I wanted to share data between all tests in given assembly &#8211; without worrying in which test class or test collection given test is. In these circumstances, I&#8217;ve usually used some kind of old fashioned singleton or custom <i>TestFrameworkExecutor<\/i>. I&#8217;ve never liked those solutions, fortunately recently I&#8217;ve come across a nice little library &#8211; <i>xunit.assemblyfixture<\/i>. <\/p>\n<h3>2. Usage<\/h3>\n<p><i>Xunit.assemblyfixture<\/i> allows you to share data between tests in given assembly via <i>IAssemblyFixture<TFixture><\/i> interface. The usage is basically the same as in other <i>XUnit&#8217;s<\/i> fixtures. All we have to do is to create a class which instance we want to share with other tests<\/p>\n<pre lang=\"csharp\">\r\npublic class TestFixture : IDisposable\r\n{\r\n    public static int InitializationCounter { get; private set; }\r\n\r\n    public TestFixture()\r\n    {\r\n        InitializationCounter++;\r\n    }\r\n\r\n    public void Dispose()\r\n    {\r\n        \/\/ free resources if necessary\r\n    }\r\n}\r\n<\/pre>\n<p>And then create test classes which implement <i>IAssemblyFixture<TFixture><\/i> interface. If we want to have access to the fixture from the tests, we can inject instance of <i>TFixture<\/i> via constructor<\/p>\n<pre lang=\"csharp\">\r\npublic class TestBase : IAssemblyFixture<TestFixture>\r\n{\r\n    protected readonly ITestOutputHelper TestOutputHelper;\r\n    protected readonly TestFixture Fixture;\r\n\r\n    protected TestBase(ITestOutputHelper testOutputHelper, TestFixture fixture)\r\n    {\r\n        this.TestOutputHelper = testOutputHelper;\r\n        this.Fixture = fixture;\r\n    }\r\n\r\n    protected void DisplayInitializationCount()\r\n    {\r\n        TestOutputHelper.WriteLine($\"Initialization count: {TestFixture.InitializationCounter}\");\r\n    }\r\n}\r\n<\/pre>\n<p><i>Xunit.assemblyfixture<\/i> fixture will ensure that given test class is shared via all the tests and is initialized only once.<br \/>\n<a href=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/02\/xunit-sharing-test-data-across-assembly\/Shared.png\" rel=\"attachment wp-att-1012\"><img decoding=\"async\" meta=\"xunit\" src=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/02\/xunit-sharing-test-data-across-assembly\/Shared.png\" alt=\"Shared\" height=\"404\" class=\"aligncenter size-full wp-image-1012\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/02\/xunit-sharing-test-data-across-assembly\/Shared.png 880w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/02\/xunit-sharing-test-data-across-assembly\/Shared-150x69.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/02\/xunit-sharing-test-data-across-assembly\/Shared-300x138.png 300w\" sizes=\"(max-width: 880px) 100vw, 880px\" \/><\/a><br \/>\nIf you want to share multiple classes across the assembly, you can, of course, use <i>IAssemblyFixture<\/i> multiple times.<\/p>\n<h3>3. Visual Studio 2017 &#8211; XUnit beta tests runners <\/h3>\n<p>As for now <i>VS 2017 RC<\/i>  requires beta runners of <i>XUnit<\/i> in order to run our unit tests. <i>xunit.assemblyfixture<\/i> seems not to cooperate with them smoothly. However, there is a simple workaround to fix that. All we have to do is to add the following attribute to our test project<\/p>\n<pre lang=\"csharp\">\r\n[assembly: TestFramework(\"Xunit.TestFramework\", \"xunit.assemblyfixture\")]\r\n<\/pre>\n<p>And from now on, <i>XUnit<\/i> correctly can inject assembly fixtures into our test classes<\/p>\n<p>Source code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/XunitSharingFixtureDataAcrossAssembly\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction XUnit provides two ways of sharing data between tests &#8211; ICollectionFixture and IClassFixture. The first one allows you to shared context across collections and the second one across test classes. I&#8217;ve already had a couple of cases in which these fixtures were not enough. Basically, I wanted to share data between all tests [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[319],"tags":[320],"class_list":["post-1011","post","type-post","status-publish","format-standard","hentry","category-xunit","tag-xunit"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=1011"}],"version-history":[{"count":9,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1011\/revisions"}],"predecessor-version":[{"id":1022,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1011\/revisions\/1022"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}