https://github.com/twenzel/nunit.extensions.helpers
Helpers to generate NUnit tests
https://github.com/twenzel/nunit.extensions.helpers
hacktoberfest nunit sourcegenerator
Last synced: about 2 months ago
JSON representation
Helpers to generate NUnit tests
- Host: GitHub
- URL: https://github.com/twenzel/nunit.extensions.helpers
- Owner: twenzel
- License: mit
- Created: 2024-09-10T18:48:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-11-18T09:45:44.000Z (5 months ago)
- Last Synced: 2025-11-18T10:11:41.116Z (5 months ago)
- Topics: hacktoberfest, nunit, sourcegenerator
- Language: C#
- Homepage:
- Size: 125 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NUnit.Extensions.Helpers
[](https://nuget.org/packages/NUnit.Extensions.Helpers/)
[](LICENSE)
[](https://github.com/twenzel/NUnit.Extensions.Helpers/actions)
[](https://sonarcloud.io/dashboard?id=twenzel_NUnit.Extensions.Helpers)
[](https://sonarcloud.io/dashboard?id=twenzel_NUnit.Extensions.Helpers)
[](https://sonarcloud.io/dashboard?id=twenzel_NUnit.Extensions.Helpers)
[](https://sonarcloud.io/dashboard?id=twenzel_NUnit.Extensions.Helpers)
[](https://sonarcloud.io/dashboard?id=twenzel_NUnit.Extensions.Helpers)
[](https://sonarcloud.io/dashboard?id=twenzel_NUnit.Extensions.Helpers)
Helpers to generate NUnit tests
> This is not an official [NUnit](https://github.com/nunit/nunit) package.
## Install
Add the NuGet package [NUnit.Extensions.Helpers](https://nuget.org/packages/NUnit.Extensions.Helpers/) to any project supporting .NET Standard 2.0 or higher.
> > dotnet add package NUnit.Extensions.Helpers
### Information
> Currently the generated source requires [Moq](https://nuget.org/packages/Moq) and [Shouldly](https://nuget.org/packages/Shouldly).
## Usage
### Constructor parameter null tests
Use the `GenerateConstructorParameterNullTests` attribute to define the SUT (class which should be tested) to generate constructor parameter tests for.
```csharp
[GenerateConstructorParameterNullTests(typeof(Document))]
internal partial class DocumentTests
{
[Test]
public void Test1()
{
Assert.Pass();
}
}
```
If the SUT looks like
```csharp
public class Document
{
private Stream _stream;
private IFileTester _fileTester;
private IOtherFilter _filter;
public Document(Stream myStream, IFileTester fileTester, IOtherFilter filter)
{
_stream = myStream ?? throw new ArgumentNullException(nameof(myStream));
_fileTester = fileTester ?? throw new ArgumentNullException(nameof(fileTester));
_filter = filter ?? throw new ArgumentNullException(nameof(filter));
}
}
```
following code will be generated:
```csharp
partial class DocumentTests
{
[Test]
public void Throws_Exception_When_MyStream_Is_Null()
{
Action action = () => new Document(null, null, null);
action.ShouldThrow().ParamName.Should().Be("myStream");
}
[Test]
public void Throws_Exception_When_FileTester_Is_Null()
{
Action action = () => new Document(Mock.Of(), null, null);
action.ShouldThrow().ParamName.Should().Be("fileTester");
}
[Test]
public void Throws_Exception_When_Filter_Is_Null()
{
Action action = () => new Document(Mock.Of(), Mock.Of(), null);
action.ShouldThrow().ParamName.Should().Be("filter");
}
}
```
#### Options
It's possible to generate a nested class with the `AsNestedClass` argument.
```csharp
[GenerateConstructorParameterNullTests(typeof(Document), AsNestedClass = true)]
internal partial class TestWithNested
{
}
```
### Web service tests
Use the `WebServiceTester` helper class. This class reads the OpenApi documentation and can execute arbitary tests.
```csharp
[Test]
public async Task TestEndpointSecurity()
{
var tester = new WebServiceTester("swagger.json");
await tester.VerifySecuredEndpointsRequiresAuthentication(httpClient, CancellationToken.None);
}
```