Skip to content

Commit c05b83e

Browse files
authored
Add TUnit sample to the docs (#175)
* Add TUnit sample to the docs * Package updates --------- Co-authored-by: JT <[email protected]>
1 parent 31454bd commit c05b83e

File tree

16 files changed

+1437
-520
lines changed

16 files changed

+1437
-520
lines changed

build/_build.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Nuke.Common" Version="8.0.0" />
15+
<PackageReference Include="Nuke.Common" Version="8.1.0" />
1616
</ItemGroup>
1717

1818
</Project>

docs/.vitepress/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export default defineConfig({
4848
function getGuideSidebar() {
4949
return [
5050
{ text: 'Alba Setup', link: '/guide/gettingstarted' },
51-
{ text: 'Integrating with xUnit.Net', link: '/guide/xunit' },
51+
{ text: 'Integrating with xUnit', link: '/guide/xunit' },
5252
{ text: 'Integrating with NUnit', link: '/guide/nunit' },
53+
{ text: 'Integrating with TUnit', link: '/guide/tunit' },
5354
{ text: 'Extension Model', link: '/guide/extensions' },
5455
{ text: 'Security Extensions', link: '/guide/security' },
5556
{ text: 'Tracing & Open Telemetry', link: '/guide/opentelemetry' },

docs/guide/nunit.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Application
1515
{
1616
Host = await AlbaHost.For<WebApp.Program>();
1717
}
18-
18+
1919
public static IAlbaHost Host { get; private set; }
2020

2121
// Make sure that NUnit will shut down the AlbaHost when
@@ -27,7 +27,7 @@ public class Application
2727
}
2828
}
2929
```
30-
<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>
30+
<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>
3131
<!-- endSnippet -->
3232

3333
Then reference the `AlbaHost` in tests like this sample:
@@ -38,15 +38,15 @@ Then reference the `AlbaHost` in tests like this sample:
3838
public class sample_integration_fixture
3939
{
4040
[Test]
41-
public Task happy_path()
41+
public async Task happy_path()
4242
{
43-
return Application.Host.Scenario(_ =>
43+
await Application.Host.Scenario(_ =>
4444
{
4545
_.Get.Url("/fake/okay");
4646
_.StatusCodeShouldBeOk();
4747
});
4848
}
4949
}
5050
```
51-
<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>
51+
<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>
5252
<!-- endSnippet -->

docs/guide/tunit.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Integrating with TUnit
2+
3+
Like other testing frameworks, you'll want to reuse the `IAlbaHost` across tests and test fixtures because
4+
`AlbaHost` is relatively expensive to create. To do that with TUnit, you should start by writing a bootstrapping class
5+
that inherits from `IAsyncInitializer` and `IAsyncDisposable`:
6+
7+
<!-- snippet: sample_TUnit_Application -->
8+
<a id='snippet-sample_tunit_application'></a>
9+
```cs
10+
public sealed class AlbaBootstrap : IAsyncInitializer, IAsyncDisposable
11+
{
12+
public IAlbaHost Host { get; private set; } = null!;
13+
14+
public async Task InitializeAsync()
15+
{
16+
Host = await AlbaHost.For<WebApp.Program>();
17+
}
18+
19+
public async ValueTask DisposeAsync()
20+
{
21+
await Host.DisposeAsync();
22+
}
23+
}
24+
```
25+
<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>
26+
<!-- endSnippet -->
27+
28+
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.
29+
30+
<!-- snippet: sample_TUnit_scenario_test -->
31+
<a id='snippet-sample_tunit_scenario_test'></a>
32+
```cs
33+
public abstract class AlbaTestBase(AlbaBootstrap albaBootstrap)
34+
{
35+
protected IAlbaHost Host => albaBootstrap.Host;
36+
}
37+
38+
[ClassDataSource<AlbaBootstrap>(Shared = SharedType.Globally)]
39+
public class MyTestClass(AlbaBootstrap albaBootstrap) : AlbaTestBase(albaBootstrap)
40+
{
41+
[Test]
42+
public async Task happy_path()
43+
{
44+
await Host.Scenario(_ =>
45+
{
46+
_.Get.Url("/fake/okay");
47+
_.StatusCodeShouldBeOk();
48+
});
49+
}
50+
}
51+
```
52+
<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>
53+
<!-- endSnippet -->

docs/guide/xunit.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Integrating Alba with xUnit.Net
1+
# Integrating with xUnit
22

33
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:
44

docs/scenarios/assertions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static Scenario ContentShouldContain(this Scenario scenario, string text)
6969
return scenario.AssertThat(new BodyContainsAssertion(text));
7070
}
7171
```
72-
<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>
72+
<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>
7373
<!-- endSnippet -->
7474

7575
Finally, use your new assertion in a Scenario like this:

0 commit comments

Comments
 (0)