Skip to content

Commit 93b4edd

Browse files
denisekartegil
andauthored
feat: Support multiple test frameworks in bUnit test template (#129) (#625)
* feat: Support multiple test frameworks in bUnit test template Add support for MSTest and NUnit test framweworks in bUnit test template refs: #129 * update verification.yml to install bunit template once * - add BunitTestContext to NUnit and MSTest template variations - inherit from BunitTestContext in NUnit and MSTest template variations - include missing namespace imports in _Imports.razor Co-authored-by: Egil Hansen <[email protected]>
1 parent 9baaad3 commit 93b4edd

File tree

9 files changed

+202
-84
lines changed

9 files changed

+202
-84
lines changed

.github/workflows/verification.yml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,35 @@ jobs:
7373
dotnet pack src/bunit/ -c release -o ${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true
7474
dotnet pack src/bunit.template/ -c release -o ${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true
7575
76-
# Excluding windows because the restore step doesnt seem to work correct.
77-
- name: ✔ Verify template
76+
- name: ✳ Install bUnit template
7877
if: matrix.os != 'windows-latest'
7978
run: |
8079
dotnet new --install bunit.template::${NBGV_NuGetPackageVersion} --nuget-source ${GITHUB_WORKSPACE}/packages
81-
dotnet new bunit --no-restore -o ${GITHUB_WORKSPACE}/TemplateTest
82-
echo '<?xml version="1.0" encoding="utf-8"?><Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>' >> ${GITHUB_WORKSPACE}/TemplateTest/Directory.Build.props
83-
dotnet restore ${GITHUB_WORKSPACE}/TemplateTest --source ${GITHUB_WORKSPACE}/packages --source https://api.nuget.org/v3/index.json
84-
dotnet test ${GITHUB_WORKSPACE}/TemplateTest --blame-hang --blame-hang-timeout 1m --blame-hang-dump-type none
80+
81+
# Excluding windows because the restore step doesnt seem to work correct.
82+
- name: ✔ Verify xUnit template
83+
if: matrix.os != 'windows-latest'
84+
run: |
85+
dotnet new bunit --no-restore -o ${GITHUB_WORKSPACE}/TemplateTestXunit
86+
echo '<?xml version="1.0" encoding="utf-8"?><Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>' >> ${GITHUB_WORKSPACE}/TemplateTestXunit/Directory.Build.props
87+
dotnet restore ${GITHUB_WORKSPACE}/TemplateTestXunit --source ${GITHUB_WORKSPACE}/packages --source https://api.nuget.org/v3/index.json
88+
dotnet test ${GITHUB_WORKSPACE}/TemplateTestXunit --blame-hang --blame-hang-timeout 1m --blame-hang-dump-type none
89+
90+
- name: ✔ Verify NUnit template
91+
if: matrix.os != 'windows-latest'
92+
run: |
93+
dotnet new bunit --framework nunit --no-restore -o ${GITHUB_WORKSPACE}/TemplateTestNunit
94+
echo '<?xml version="1.0" encoding="utf-8"?><Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>' >> ${GITHUB_WORKSPACE}/TemplateTestNunit/Directory.Build.props
95+
dotnet restore ${GITHUB_WORKSPACE}/TemplateTestNunit --source ${GITHUB_WORKSPACE}/packages --source https://api.nuget.org/v3/index.json
96+
dotnet test ${GITHUB_WORKSPACE}/TemplateTestNunit --blame-hang --blame-hang-timeout 1m --blame-hang-dump-type none
97+
98+
- name: ✔ Verify MSTest template
99+
if: matrix.os != 'windows-latest'
100+
run: |
101+
dotnet new bunit --framework mstest --no-restore -o ${GITHUB_WORKSPACE}/TemplateTestMstest
102+
echo '<?xml version="1.0" encoding="utf-8"?><Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>' >> ${GITHUB_WORKSPACE}/TemplateTestMstest/Directory.Build.props
103+
dotnet restore ${GITHUB_WORKSPACE}/TemplateTestMstest --source ${GITHUB_WORKSPACE}/packages --source https://api.nuget.org/v3/index.json
104+
dotnet test ${GITHUB_WORKSPACE}/TemplateTestMstest --blame-hang --blame-hang-timeout 1m --blame-hang-dump-type none
85105
86106
# DocFx only works well on Windows currently
87107
- name: 📄 Build documentation

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
1111
- Added `FakeSignOutSessionStateManage` type in Blazor, that makes it easy to test components that use the `SignOutSessionStateManage` type. By [@linkdotnet](https://github.com/linkdotnet).
1212
- Added a validation to `AddChildContent` method in `ComponentParameterCollectionBuilder` that will throw an exception if the component's `ChildContent` is a generic type. By [@denisekart](https://github.com/denisekart).
1313
- Added more optional arguments for `Click` and `DoubleClick` extensions which were introduced in .NET 5 and .NET 6. By [@linkdotnet](https://github.com/linkdotnet).
14+
- Added template support for `Nunit` and `MSTest` unit test frameworks. By [@denisekart](https://github.com/denisekart).
1415

1516
### Fixed
1617

src/bunit.template/template/.template.config/dotnetcli.host.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
"skipRestore": {
44
"longName": "no-restore",
55
"shortName": ""
6+
},
7+
"UnitTestFramework": {
8+
"longName": "framework",
9+
"shortName": ""
610
}
7-
}
8-
}
11+
},
12+
"usageExamples": [
13+
"--framework xunit",
14+
"--framework nunit",
15+
"--framework mstest"
16+
]
17+
}
Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"$schema": "http://json.schemastore.org/template",
33
"author": "Egil Hansen",
44
"classifications": [
@@ -18,75 +18,76 @@
1818
},
1919
"sourceName": "Company.BlazorTests1",
2020
"defaultName": "BlazorTestProject1",
21-
"preferNameDirectory": true,
22-
"symbols": {
23-
"HostIdentifier": {
24-
"type": "bind",
25-
"binding": "HostIdentifier"
26-
},
27-
"skipRestore": {
28-
"type": "parameter",
29-
"datatype": "bool",
30-
"description": "If specified, skips the automatic restore of the project on create.",
31-
"defaultValue": "false"
32-
}
33-
},
21+
"preferNameDirectory": true,
22+
"sources": [
23+
{
24+
"modifiers": [
25+
{
26+
"exclude": [ "BunitTestContext.cs" ],
27+
"condition": "(testFramework_xunit)"
28+
}
29+
]
30+
}
31+
],
32+
"symbols": {
33+
"HostIdentifier": {
34+
"type": "bind",
35+
"binding": "HostIdentifier"
36+
},
37+
"skipRestore": {
38+
"type": "parameter",
39+
"datatype": "bool",
40+
"description": "If specified, skips the automatic restore of the project on create.",
41+
"defaultValue": "false"
42+
},
43+
"UnitTestFramework": {
44+
"type": "parameter",
45+
"description": "The target unit testing framework for the project.",
46+
"displayName": "Unit test framework",
47+
"datatype": "choice",
48+
"defaultValue": "xunit",
49+
"replaces": "UnitTestFramework",
50+
"choices": [
51+
{
52+
"choice": "nunit",
53+
"description": "NUnit unit testing framework",
54+
"displayName": "NUnit"
55+
},
56+
{
57+
"choice": "xunit",
58+
"description": "xUnit unit testing framework",
59+
"displayName": "xUnit"
60+
},
61+
{
62+
"choice": "mstest",
63+
"description": "MSTest unit testing framework",
64+
"displayName": "MSTest"
65+
}
66+
]
67+
},
68+
"testFramework_nunit": {
69+
"type": "computed",
70+
"value": "UnitTestFramework == \"nunit\""
71+
},
72+
"testFramework_xunit": {
73+
"type": "computed",
74+
"value": "UnitTestFramework == \"xunit\""
75+
},
76+
"testFramework_mstest": {
77+
"type": "computed",
78+
"value": "UnitTestFramework == \"mstest\""
79+
}
80+
},
3481
"primaryOutputs": [
3582
{ "path": "Company.BlazorTests1.csproj" }
3683
],
3784
"postActions": [
38-
{
39-
"condition": "(!skipRestore)",
40-
"description": "Restore NuGet packages required by this project.",
41-
"manualInstructions": [ { "text": "Run 'dotnet restore'" } ],
42-
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
43-
"continueOnError": true
44-
}
45-
//},
46-
//{
47-
// "description": "Adding latests bUnit.Core reference",
48-
// "actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
49-
// "continueOnError": true,
50-
// "manualInstructions": [
51-
// {
52-
// "text": "Manually add the 'bunit.core' reference to your project file"
53-
// }
54-
// ],
55-
// "args": {
56-
// "referenceType": "package",
57-
// "reference": "bunit.core",
58-
// "projectFileExtensions": ".csproj"
59-
// }
60-
//},
61-
//{
62-
// "description": "Adding latests bUnit.Web reference",
63-
// "actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
64-
// "continueOnError": true,
65-
// "manualInstructions": [
66-
// {
67-
// "text": "Manually add the 'bunit.web' reference to your project file"
68-
// }
69-
// ],
70-
// "args": {
71-
// "referenceType": "package",
72-
// "reference": "bunit.web",
73-
// "projectFileExtensions": ".csproj"
74-
// }
75-
//},
76-
//{
77-
// "description": "Adding latests bUnit.xUnit reference",
78-
// "actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
79-
// "continueOnError": true,
80-
// "manualInstructions": [
81-
// {
82-
// "text": "Manually add the 'bunit.xunit' reference to your project file"
83-
// }
84-
// ],
85-
// "args": {
86-
// "referenceType": "package",
87-
// "reference": "bunit.xunit",
88-
// "projectFileExtensions": ".csproj"
89-
// }
90-
//}
85+
{
86+
"condition": "(!skipRestore)",
87+
"description": "Restore NuGet packages required by this project.",
88+
"manualInstructions": [ { "text": "Run 'dotnet restore'" } ],
89+
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
90+
"continueOnError": true
91+
}
9192
]
9293
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Bunit;
2+
#if (testFramework_nunit)
3+
using NUnit.Framework;
4+
#elif (testFramework_mstest)
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
#endif
7+
8+
namespace Company.BlazorTests1;
9+
10+
/// <summary>
11+
/// Test context wrapper for bUnit.
12+
/// Read more about using <see cref="BunitTestContext"/> <seealso href="https://bunit.dev/docs/getting-started/writing-tests.html#remove-boilerplate-code-from-tests">here</seealso>.
13+
/// </summary>
14+
public abstract class BunitTestContext : TestContextWrapper
15+
{
16+
#if (testFramework_nunit)
17+
[SetUp]
18+
#elif (testFramework_mstest)
19+
[TestInitialize]
20+
#endif
21+
public void Setup() => TestContext = new Bunit.TestContext();
22+
23+
#if (testFramework_nunit)
24+
[TearDown]
25+
#elif (testFramework_mstest)
26+
[TestCleanup]
27+
#endif
28+
public void TearDown() => TestContext?.Dispose();
29+
}

src/bunit.template/template/Company.BlazorTests1.csproj

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,37 @@
99
<ItemGroup>
1010
<Using Include="Bunit" />
1111
<Using Include="Bunit.TestDoubles" />
12-
<Using Include="Xunit" />
1312
<Using Include="Microsoft.Extensions.DependencyInjection" />
13+
<Using Include="Xunit" Condition="'$(testFramework_xunit)' == 'true'"/>
14+
<Using Include="NUnit.Framework" Condition="'$(testFramework_nunit)' == 'true'"/>
15+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" Condition="'$(testFramework_mstest)' == 'true'"/>
1416
</ItemGroup>
1517

1618
<ItemGroup>
17-
<PackageReference Include="bunit" Version="#{NBGV_NuGetPackageVersion}#" />
19+
<PackageReference Include="bunit" Version="#{NBGV_NuGetPackageVersion}#" />
1820
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
19-
<PackageReference Include="xunit" Version="2.4.1" />
20-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
21+
<PackageReference Include="coverlet.collector" Version="3.1.0">
2122
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2223
<PrivateAssets>all</PrivateAssets>
2324
</PackageReference>
24-
<PackageReference Include="coverlet.collector" Version="3.1.0">
25+
</ItemGroup>
26+
27+
<ItemGroup Condition="'$(testFramework_xunit)' == 'true'">
28+
<PackageReference Include="xunit" Version="2.4.1" />
29+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
2530
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2631
<PrivateAssets>all</PrivateAssets>
2732
</PackageReference>
28-
</ItemGroup>
33+
</ItemGroup>
34+
35+
<ItemGroup Condition="'$(testFramework_nunit)' == 'true'">
36+
<PackageReference Include="NUnit" Version="3.13.2" />
37+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
38+
</ItemGroup>
39+
40+
<ItemGroup Condition="'$(testFramework_mstest)' == 'true'">
41+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
42+
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
43+
</ItemGroup>
2944

3045
</Project>

src/bunit.template/template/CounterCSharpTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@ namespace Company.BlazorTests1;
44
/// These tests are written entirely in C#.
55
/// Learn more at https://bunit.dev/docs/getting-started/writing-tests.html#creating-basic-tests-in-cs-files
66
/// </summary>
7+
#if (testFramework_xunit)
78
public class CounterCSharpTests : TestContext
9+
#elif (testFramework_nunit)
10+
public class CounterCSharpTests : BunitTestContext
11+
#elif (testFramework_mstest)
12+
[TestClass]
13+
public class CounterCSharpTests : BunitTestContext
14+
#endif
815
{
16+
#if (testFramework_xunit)
917
[Fact]
18+
#elif (testFramework_nunit)
19+
[Test]
20+
#elif (testFramework_mstest)
21+
[TestMethod]
22+
#endif
1023
public void CounterStartsAtZero()
1124
{
1225
// Arrange
@@ -16,7 +29,13 @@ public void CounterStartsAtZero()
1629
cut.Find("p").MarkupMatches("<p>Current count: 0</p>");
1730
}
1831

32+
#if (testFramework_xunit)
1933
[Fact]
34+
#elif (testFramework_nunit)
35+
[Test]
36+
#elif (testFramework_mstest)
37+
[TestMethod]
38+
#endif
2039
public void ClickingButtonIncrementsCounter()
2140
{
2241
// Arrange

src/bunit.template/template/CounterRazorTests.razor

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
@*#if (testFramework_xunit)*@
12
@inherits TestContext
3+
@*#elif (testFramework_nunit)*@
4+
@inherits BunitTestContext
5+
@*#elif (testFramework_mstest)*@
6+
@attribute [TestClass]
7+
@inherits BunitTestContext
8+
@*#endif*@
29

310
These tests are written entirely in razor and C# syntax.
411

512
Learn more at https://bunit.dev/docs/getting-started/writing-tests.html#creating-basic-tests-in-razor-files
613

714
@code {
8-
[Fact]
15+
@*#if (testFramework_xunit)*@
16+
[Fact]
17+
@*#elif (testFramework_nunit)*@
18+
[Test]
19+
@*#elif (testFramework_mstest)*@
20+
[TestMethod]
21+
@*#endif*@
922
public void CounterStartsAtZero()
1023
{
1124
// Arrange
@@ -14,8 +27,13 @@ Learn more at https://bunit.dev/docs/getting-started/writing-tests.html#creating
1427
// Assert that content of the paragraph shows counter at zero
1528
cut.Find("p").MarkupMatches(@<p>Current count: 0</p>);
1629
}
17-
18-
[Fact]
30+
@*#if (testFramework_xunit)*@
31+
[Fact]
32+
@*#elif (testFramework_nunit)*@
33+
[Test]
34+
@*#elif (testFramework_mstest)*@
35+
[TestMethod]
36+
@*#endif*@
1937
public void ClickingButtonIncrementsCounter()
2038
{
2139
// Arrange

src/bunit.template/template/_Imports.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
@using Microsoft.Extensions.DependencyInjection
44
@using Bunit
55
@using Bunit.TestDoubles
6+
@*#if (testFramework_xunit)*@
67
@using Xunit
8+
@*#elif (testFramework_nunit)*@
9+
@using NUnit.Framework
10+
@*#elif (testFramework_mstest)*@
11+
@using Microsoft.VisualStudio.TestTools.UnitTesting
12+
@*#endif*@

0 commit comments

Comments
 (0)