Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TUnit sample to the docs #175

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="8.0.0" />
<PackageReference Include="Nuke.Common" Version="8.1.0" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export default defineConfig({
function getGuideSidebar() {
return [
{ text: 'Alba Setup', link: '/guide/gettingstarted' },
{ text: 'Integrating with xUnit.Net', link: '/guide/xunit' },
{ text: 'Integrating with xUnit', link: '/guide/xunit' },
{ text: 'Integrating with NUnit', link: '/guide/nunit' },
{ text: 'Integrating with TUnit', link: '/guide/tunit' },
{ text: 'Extension Model', link: '/guide/extensions' },
{ text: 'Security Extensions', link: '/guide/security' },
{ text: 'Tracing & Open Telemetry', link: '/guide/opentelemetry' },
Expand Down
10 changes: 5 additions & 5 deletions docs/guide/nunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Application
{
Host = await AlbaHost.For<WebApp.Program>();
}

public static IAlbaHost Host { get; private set; }

// Make sure that NUnit will shut down the AlbaHost when
Expand All @@ -27,7 +27,7 @@ public class Application
}
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/NUnitSamples/UnitTest1.cs#L8-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nunit_application' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/NUnitSamples/UnitTest1.cs#L7-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nunit_application' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Then reference the `AlbaHost` in tests like this sample:
Expand All @@ -38,15 +38,15 @@ Then reference the `AlbaHost` in tests like this sample:
public class sample_integration_fixture
{
[Test]
public Task happy_path()
public async Task happy_path()
{
return Application.Host.Scenario(_ =>
await Application.Host.Scenario(_ =>
{
_.Get.Url("/fake/okay");
_.StatusCodeShouldBeOk();
});
}
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/NUnitSamples/UnitTest1.cs#L32-L45' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nunit_scenario_test' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/NUnitSamples/UnitTest1.cs#L31-L44' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_nunit_scenario_test' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
53 changes: 53 additions & 0 deletions docs/guide/tunit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Integrating with TUnit

Like other testing frameworks, you'll want to reuse the `IAlbaHost` across tests and test fixtures because
`AlbaHost` is relatively expensive to create. To do that with TUnit, you should start by writing a bootstrapping class
that inherits from `IAsyncInitializer` and `IAsyncDisposable`:

<!-- snippet: sample_TUnit_Application -->
<a id='snippet-sample_tunit_application'></a>
```cs
public sealed class AlbaBootstrap : IAsyncInitializer, IAsyncDisposable
{
public IAlbaHost Host { get; private set; } = null!;

public async Task InitializeAsync()
{
Host = await AlbaHost.For<WebApp.Program>();
}

public async ValueTask DisposeAsync()
{
await Host.DisposeAsync();
}
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/TUnitSamples/Program.cs#L6-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_tunit_application' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Then inject the instance by added `[ClassDataSource<AlbaBootstrap>(Shared = SharedType.Globally)]` to your test class. We recommend creating a base class to allow easier access of the host and any other dependencies.

<!-- snippet: sample_TUnit_scenario_test -->
<a id='snippet-sample_tunit_scenario_test'></a>
```cs
public abstract class AlbaTestBase(AlbaBootstrap albaBootstrap)
{
protected IAlbaHost Host => albaBootstrap.Host;
}

[ClassDataSource<AlbaBootstrap>(Shared = SharedType.Globally)]
public class MyTestClass(AlbaBootstrap albaBootstrap) : AlbaTestBase(albaBootstrap)
{
[Test]
public async Task happy_path()
{
await Host.Scenario(_ =>
{
_.Get.Url("/fake/okay");
_.StatusCodeShouldBeOk();
});
}
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/TUnitSamples/Program.cs#L23-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_tunit_scenario_test' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/guide/xunit.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Integrating Alba with xUnit.Net
# Integrating with xUnit

If you are writing only a few Alba specifications in your testing project and your application spins up very quickly, you can just happily write tests like this:

Expand Down
2 changes: 1 addition & 1 deletion docs/scenarios/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static Scenario ContentShouldContain(this Scenario scenario, string text)
return scenario.AssertThat(new BodyContainsAssertion(text));
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba/ScenarioExpectationsExtensions.cs#L8-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_contentshouldcontain' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba/ScenarioExpectationsExtensions.cs#L8-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_contentshouldcontain' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Finally, use your new assertion in a Scenario like this:
Expand Down
Loading
Loading