https://github.com/sharpjs/subatomix.testing
Preferred frameworks for automated testing in .NET
https://github.com/sharpjs/subatomix.testing
dotnet testing
Last synced: 10 months ago
JSON representation
Preferred frameworks for automated testing in .NET
- Host: GitHub
- URL: https://github.com/sharpjs/subatomix.testing
- Owner: sharpjs
- License: mit
- Created: 2020-10-31T18:44:49.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-02-23T23:31:35.000Z (11 months ago)
- Last Synced: 2025-02-23T23:35:05.835Z (11 months ago)
- Topics: dotnet, testing
- Language: C#
- Homepage:
- Size: 273 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## About
[](https://github.com/sharpjs/Subatomix.Testing/actions)
[](https://github.com/sharpjs/Mixer/actions)
[](https://www.nuget.org/packages/Subatomix.Testing)
[](https://www.nuget.org/packages/Subatomix.Testing)
My preferred frameworks for automated testing in .NET.
See [here](https://github.com/sharpjs/Subatomix.Testing/blob/main/Subatomix.Testing/Subatomix.Testing.csproj)
for a list of included packages and their versions.
This package also includes the
[`TestHarnessBase`](https://github.com/sharpjs/Subatomix.Testing/blob/main/Subatomix.Testing/TestHarnessBase.cs)
class, which aids my preferred technique for setup/teardown code. Generally, I
eschew traditional `SetUp` and `TearDown` methods. Instead, in each test, I
create an instance of a disposable context class. Construction is setup, and
disposal is teardown. Because `TestContext` means something else already in
NUnit, I call this pattern *Test Harness* instead.
```csharp
[Test]
public void TestSomething()
{
using var h = new TestHarness();
// rest of test
}
private class TestHarness : TestHarnessBase
{
// properties for mocks and things
public TestHarness()
{
// setup code
}
protected override CleanUp()
{
// teardown code
}
}
```
This pattern enables some cool things:
- I can enable the C# 8
[nullability checker](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references)
and not have to sprinkle `?` and `!` all over the test code.
- Tests can run in parallel, regardless of test fixture lifetime, since there
is no longer any shared state within a test class.
- Test-support code can be isolated away from the tests themselves.
If the
[test fixture lifetime](https://docs.nunit.org/articles/nunit/writing-tests/attributes/fixturelifecycle.html)
is instance-per-test-case, the test fixture itself can be a subclass of
`TestHarnessBase`. This results in a test fixture more closely resembling a
traditional one and removes the need for a `using` statement in each test,
while retaining the improved nullability ergonomics. However, directly
subclassing `TestHarnessBase` forfeits the isolation afforded by having a
separate test harness class.
```csharp
[TestFixture]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class SomeTests : TestHarnessBase
{
// properties for mocks and things
public TestHarness()
{
// setup code
}
protected override CleanUp()
{
// teardown code
}
[Test]
public void TestSomething()
{
// test
}
}
```