{"id":18602978,"url":"https://github.com/devlooped/xunit.assemblyfixture","last_synced_at":"2025-04-10T19:31:21.394Z","repository":{"id":60774080,"uuid":"43914169","full_name":"devlooped/xunit.assemblyfixture","owner":"devlooped","description":"Provides per-assembly fixture state support for xunit","archived":false,"fork":false,"pushed_at":"2025-03-15T00:09:46.000Z","size":70,"stargazers_count":26,"open_issues_count":8,"forks_count":12,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T03:34:03.516Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devlooped.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"devlooped"}},"created_at":"2015-10-08T20:19:00.000Z","updated_at":"2025-02-07T20:00:21.000Z","dependencies_parsed_at":"2024-06-19T09:17:26.110Z","dependency_job_id":"c8f02a62-d561-4e8b-b6ca-7f364875a9b7","html_url":"https://github.com/devlooped/xunit.assemblyfixture","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devlooped%2Fxunit.assemblyfixture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devlooped%2Fxunit.assemblyfixture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devlooped%2Fxunit.assemblyfixture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devlooped%2Fxunit.assemblyfixture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devlooped","download_url":"https://codeload.github.com/devlooped/xunit.assemblyfixture/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281399,"owners_count":21077423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T02:13:08.611Z","updated_at":"2025-04-10T19:31:16.379Z","avatar_url":"https://github.com/devlooped.png","language":"C#","funding_links":["https://github.com/sponsors/devlooped"],"categories":[],"sub_categories":[],"readme":"# xunit.assemblyfixture\n\nProvides shared state/fixture data across tests in the same assembly, following the design of \n[class fixtures](https://xunit.github.io/docs/shared-context.html#class-fixture) (rather than the more \nconvoluted [collection fixtures](https://xunit.github.io/docs/shared-context.html#collection-fixture)). \n\nTo complement [xUnit documentation style](https://xunit.github.io/docs/shared-context.html), I shamelessly copy its layout here.\n\n\u003e NOTE: applies to xunit 2.4 only. 3.0+ will have its own way of doing this.\n\n## Shared Context between Tests\n\nPlease read [xUnit documentation](https://xunit.github.io/docs/shared-context.html) on shared context and the various built-in options, which are:\n\n- [Constructor and Dispose](https://xunit.github.io/docs/shared-context.html#constructor) (shared setup/cleanup code without sharing object instances)\n- [Class Fixtures](https://xunit.github.io/docs/shared-context.html#class-fixture) (shared object instance across tests in a single class)\n- [Collection Fixtures](https://xunit.github.io/docs/shared-context.html#collection-fixture) (shared object instances across multiple test classes\n\nTo which this project adds:\n\n- Assembly Fixtures (shared object instances across multiple test classes within the same test assembly)\n\n### Assembly Fixtures\n\n***When to use***: when you want to create a single assembly-level context\n  and share it among all tests in the assembly, and have it cleaned up after\n  all the tests in the assembly have finished.\n\n  Sometimes test context creation and cleanup can be very expensive. If you were\n  to run the creation and cleanup code during every test, it might make the tests\n  slower than you want. Sometimes, you just need to aggregate data across multiple \n  tests in multiple classes. You can use the *assembly fixture* feature of\n  [xUnit.net [Assembly Fixtures]](https://www.nuget.org/packages/xunit.assemblyfixture) \n  to share a single object instance among all tests in a test assembly.\n  \n  When using an assembly fixture, xUnit.net will ensure that the fixture instance \n  will be created before any of the tests using it have run, and once all the tests \n  have finished, it will clean up the fixture object by calling `Dispose`, if present.\n\nTo use assembly fixtures, you need to take the following steps:\n\n- Create the fixture class, and put the the startup code in the fixture\n  class constructor.\n- If the fixture class needs to perform cleanup, implement `IDisposable`\n  on the fixture class, and put the cleanup code in the `Dispose()` method.\n- Add `IAssemblyFixture\u003cTFixture\u003e` to the test class.\n- If the test class needs access to the fixture instance, add it as a\n  constructor argument, and it will be provided automatically.\n\nHere is a simple example:\n\n```csharp\npublic class DatabaseFixture : IDisposable\n{\n    public DatabaseFixture()\n    {\n        Db = new SqlConnection(\"MyConnectionString\");\n\n        // ... initialize data in the test database ...\n    }\n\n    public void Dispose()\n    {\n        // ... clean up test data from the database ...\n    }\n\n    public SqlConnection Db { get; private set; }\n}\n\npublic class MyDatabaseTests : IAssemblyFixture\u003cDatabaseFixture\u003e\n{\n    DatabaseFixture fixture;\n\n    public MyDatabaseTests(DatabaseFixture fixture)\n    {\n        this.fixture = fixture;\n    }\n\n    // ... write tests, using fixture.Db to get access to the SQL Server ...\n}\n```\n\n  Just before the first test in the assembly to require the `DatabaseFixture`\n  is run, xUnit.net will create an instance of `DatabaseFixture`. Each subsequent \n  test will receive the same shared instance, passed to the constructor of \n  `MyDatabaseTests`, just like a static singleton, but with predictable cleanup\n  via `IDisposable`.  \n\n  ***Important note:*** xUnit.net uses the presence of the interface\n  `IAssemblyFixture\u003c\u003e` to know that you want an assembly fixture to\n  be created and cleaned up. It will do this whether you take the instance of\n  the class as a constructor argument or not. Simiarly, if you add the constructor\n  argument but forget to add the interface, xUnit.net will let you know that it\n  does not know how to satisfy the constructor argument.\n\n  If you need multiple fixture objects, you can implement the interface as many\n  times as you want, and add constructor arguments for whichever of the fixture\n  object instances you need access to. The order of the constructor arguments\n  is unimportant.\n\n  Note that you cannot control the order that fixture objects are created, and\n  fixtures cannot take dependencies on other fixtures. If you have need to\n  control creation order and/or have dependencies between fixtures, you should\n  create a class which encapsulates the other two fixtures, so that it can\n  do the object creation itself.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevlooped%2Fxunit.assemblyfixture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevlooped%2Fxunit.assemblyfixture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevlooped%2Fxunit.assemblyfixture/lists"}