Skip to content

Commit 56a6e72

Browse files
authored
Xunit RazorTest runner (#103)
Custom xUnit discoverer and test runner for razor based tests.
1 parent 91b4ac8 commit 56a6e72

138 files changed

Lines changed: 1421 additions & 345 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/CI.yml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
echo "::set-env name=BRANCH::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')"
2222
echo "::set-env name=COMMIT::$GITHUB_SHA"
2323
- run: echo Version $VERSION - Branch $BRANCH - Commit $COMMIT
24-
- uses: actions/checkout@v1
24+
- uses: actions/checkout@v2
2525
- name: Update tokens in project files
2626
uses: cschleiden/replace-tokens@v1
2727
with:
@@ -31,15 +31,20 @@ jobs:
3131
dotnet-version: '3.1.201'
3232
- name: Building and verifying library
3333
run: |
34-
dotnet build src -c Release
35-
dotnet test src -c Release /nowarn:CS1591
34+
dotnet restore
35+
dotnet build src -c Release --no-restore
36+
dotnet test src -c Release --no-restore --verbosity normal /nowarn:CS1591
3637
- name: Creating library package
37-
run: dotnet pack src/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
38-
- name: Buidling template package
39-
run: dotnet pack templates/bunit-project/ -c Release -o ${GITHUB_WORKSPACE}/templates -p:version=$VERSION -p:PackageVersion=$VERSION
40-
- name: Verifying template
4138
run: |
42-
dotnet new --install ${GITHUB_WORKSPACE}/templates/bunit.template.$VERSION.nupkg
43-
dotnet new bunit -o ${GITHUB_WORKSPACE}/Test
44-
dotnet restore ${GITHUB_WORKSPACE}/Test/Test.csproj --source ${GITHUB_WORKSPACE}/lib
45-
dotnet test ${GITHUB_WORKSPACE}/Test
39+
dotnet pack src/bunit.core/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
40+
dotnet pack src/bunit.web/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
41+
dotnet pack src/bunit.xunit/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
42+
dotnet pack src/bunit/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
43+
# - name: Buidling template package
44+
# run: dotnet pack templates/bunit-project/ -c Release -o ${GITHUB_WORKSPACE}/templates -p:version=$VERSION -p:PackageVersion=$VERSION
45+
# - name: Verifying template
46+
# run: |
47+
# dotnet new --install ${GITHUB_WORKSPACE}/templates/bunit.template.$VERSION.nupkg
48+
# dotnet new bunit -o ${GITHUB_WORKSPACE}/Test
49+
# dotnet restore ${GITHUB_WORKSPACE}/Test/Test.csproj --source ${GITHUB_WORKSPACE}/lib
50+
# dotnet test ${GITHUB_WORKSPACE}/Test

.github/workflows/nuget-pack-push.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ jobs:
3535
dotnet build src -c Release /nowarn:CS1591
3636
dotnet test src -c Release /nowarn:CS1591
3737
- name: Creating library package
38-
run: dotnet pack src/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
38+
run: |
39+
dotnet pack src/bunit.core/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
40+
dotnet pack src/bunit.web/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
41+
dotnet pack src/bunit.xunit/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
42+
dotnet pack src/bunit/ -c Release -o ${GITHUB_WORKSPACE}/lib -p:version=$VERSION /nowarn:CS1591
3943
- name: Buidling template package
4044
run: dotnet pack template/ -c Release -o ${GITHUB_WORKSPACE}/template -p:version=$VERSION -p:PackageVersion=$VERSION
4145
- name: Verifying template

src/Directory.Build.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
<LangVersion>8.0</LangVersion>
44
<Nullable>enable</Nullable>
55
<WarningsAsErrors>CS8600;CS8602;CS8603;CS8625</WarningsAsErrors>
6+
<Deterministic>true</Deterministic>
67
</PropertyGroup>
78

89
<ItemGroup>
9-
<PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0-beta3.final">
10+
<PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
1011
<PrivateAssets>all</PrivateAssets>
1112
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1213
</PackageReference>

src/bunit.core.tests/ComponentParameterBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using Bunit.Rendering;
4-
using Bunit.SampleComponents;
4+
using Bunit.TestAssets.SampleComponents;
55
using Microsoft.AspNetCore.Components;
66
using Shouldly;
77
using Xunit;

src/bunit.core.tests/TestRendererTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading.Tasks;
44
using Bunit.Rendering;
55
using Bunit.Rendering.RenderEvents;
6-
using Bunit.SampleComponents;
6+
using Bunit.TestAssets.SampleComponents;
77
using Microsoft.AspNetCore.Components;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging.Abstractions;

src/bunit.core.tests/bunit.core.tests.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.*" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.*" />
1113
<PackageReference Include="AngleSharp" Version="0.14.0" />
1214
<PackageReference Include="AngleSharp.Css" Version="0.14.0" />
1315
<PackageReference Include="AngleSharp.Diffing" Version="0.14.0" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
15-
<PackageReference Include="Moq" Version="4.13.1" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.0" />
17+
<PackageReference Include="Moq" Version="4.14.1" />
1618
<PackageReference Include="Shouldly" Version="4.0.0-beta0002" />
1719
<PackageReference Include="xunit" Version="2.4.1" />
1820
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
@@ -26,7 +28,8 @@
2628
</ItemGroup>
2729

2830
<ItemGroup>
29-
<ProjectReference Include="..\bunit.core\bunit.core.csproj" />
31+
<ProjectReference Include="..\bunit.core\bunit.core.csproj" />
32+
<ProjectReference Include="..\bunit.testassets\bunit.testassets.csproj" />
3033
</ItemGroup>
3134

3235
</Project>

src/bunit.core/RazorTesting/ComponentUnderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Bunit
66
{
77
/// <summary>
8-
/// Represents a component that can be added inside a <see cref="RazorTest"/>,
8+
/// Represents a component that can be added inside a <see cref="RazorTestBase"/>,
99
/// where a component under test can be defined as the child content.
1010
/// </summary>
1111
public class ComponentUnderTest : FragmentBase

src/bunit.core/RazorTesting/FixtureBase.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4+
45
using Bunit.RazorTesting;
56
using Bunit.Rendering;
7+
68
using Microsoft.AspNetCore.Components;
79
using Microsoft.Extensions.DependencyInjection;
810

@@ -13,7 +15,7 @@ namespace Bunit
1315
/// define the <see cref="ComponentUnderTest"/> and any <see cref="Fragment"/>'s
1416
/// you might need during testing, and assert against them in the Test methods.
1517
/// </summary>
16-
public abstract class FixtureBase<TFixture> : RazorTest
18+
public abstract class FixtureBase<TFixture> : RazorTestBase
1719
{
1820
/// <summary>
1921
/// Gets or sets the child content of the fragment.
@@ -73,24 +75,31 @@ public abstract class FixtureBase<TFixture> : RazorTest
7375
/// <inheritdoc/>
7476
public override Task SetParametersAsync(ParameterView parameters)
7577
{
76-
if (parameters.TryGetValue<RenderFragment>(nameof(ChildContent), out RenderFragment childContent))
77-
ChildContent = childContent;
78-
else
79-
throw new InvalidOperationException($"No {nameof(ChildContent)} specified in the {GetType().Name} component.");
80-
78+
ChildContent = parameters.GetValueOrDefault<RenderFragment>(nameof(ChildContent));
8179
Setup = parameters.GetValueOrDefault<Action<TFixture>>(nameof(Setup));
8280
SetupAsync = parameters.GetValueOrDefault<Func<TFixture, Task>>(nameof(SetupAsync));
8381
Test = parameters.GetValueOrDefault<Action<TFixture>>(nameof(Test));
8482
TestAsync = parameters.GetValueOrDefault<Func<TFixture, Task>>(nameof(TestAsync));
85-
Tests = parameters.GetValueOrDefault<IReadOnlyCollection<Action<TFixture>>>(nameof(Tests));
86-
TestsAsync = parameters.GetValueOrDefault<IReadOnlyCollection<Func<TFixture, Task>>>(nameof(TestsAsync));
83+
Tests = parameters.GetValueOrDefault<IReadOnlyCollection<Action<TFixture>>>(nameof(Tests), Array.Empty<Action<TFixture>>());
84+
TestsAsync = parameters.GetValueOrDefault<IReadOnlyCollection<Func<TFixture, Task>>>(nameof(TestsAsync), Array.Empty<Func<TFixture, Task>>());
8785

8886
return base.SetParametersAsync(parameters);
8987
}
9088

89+
/// <inheritdoc/>
90+
public override void Validate()
91+
{
92+
base.Validate();
93+
if (ChildContent is null)
94+
throw new ArgumentException($"No {nameof(ChildContent)} specified in the {GetType().Name} component.");
95+
if (Test is null && TestAsync is null && Tests?.Count == 0 && TestsAsync?.Count == 0)
96+
throw new ArgumentException($"No test/assertions provided to the {GetType().Name} component.");
97+
}
98+
9199
/// <inheritdoc/>
92100
protected virtual async Task Run(TFixture self)
93101
{
102+
Validate();
94103
if (Setup is { })
95104
TryRun(Setup, self);
96105

src/bunit.core/RazorTesting/RazorTest.cs renamed to src/bunit.core/RazorTesting/RazorTestBase.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Threading.Tasks;
3+
34
using Microsoft.AspNetCore.Components;
45

56
namespace Bunit.RazorTesting
67
{
78
/// <summary>
89
/// Represents a component used to define tests in Razor files.
910
/// </summary>
10-
public abstract class RazorTest : TestContextBase, ITestContext, IComponent
11+
public abstract class RazorTestBase : TestContextBase, ITestContext, IComponent
1112
{
1213
/// <summary>
1314
/// Gets whether the tests is running or not.
@@ -30,14 +31,14 @@ public abstract class RazorTest : TestContextBase, ITestContext, IComponent
3031
[Parameter] public int Timeout { get; set; } = 0;
3132

3233
/// <summary>
33-
/// Run the test logic of the <see cref="RazorTest"/>.
34+
/// Run the test logic of the <see cref="RazorTestBase"/>.
3435
/// </summary>
3536
/// <exception cref="InvalidOperationException">Thrown when called and <see cref="IsRunning"/> is true.</exception>
3637
/// <returns></returns>
3738
public async Task RunTest()
3839
{
3940
if (IsRunning)
40-
throw new InvalidOperationException("The fixture test is already running.");
41+
throw new InvalidOperationException("The razor test is already running.");
4142

4243
IsRunning = true;
4344

@@ -51,17 +52,21 @@ public async Task RunTest()
5152
}
5253
}
5354

54-
/// <inheritdoc/>
55-
public override string ToString() => $"{Description ?? "[no description]"}";
56-
5755
/// <inheritdoc/>
5856
public virtual Task SetParametersAsync(ParameterView parameters)
5957
{
6058
Skip = parameters.GetValueOrDefault<string>(nameof(Skip));
6159
Description = parameters.GetValueOrDefault<string>(nameof(Description));
60+
Timeout = parameters.GetValueOrDefault<int>(nameof(Timeout));
6261
return Task.CompletedTask;
6362
}
6463

64+
/// <summary>
65+
/// Validates the test and throws an exception if the test does not have received all
66+
/// input it needs to run.
67+
/// </summary>
68+
public virtual void Validate() { }
69+
6570
/// <inheritdoc/>
6671
void IComponent.Attach(RenderHandle renderHandle) { }
6772

src/bunit.core/RazorTesting/RazorTestFailedException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Bunit.RazorTesting
55
{
66
/// <summary>
7-
/// Exception thrown when an <see cref="RazorTest"/> in a Razor based test fails.
7+
/// Exception thrown when an <see cref="RazorTestBase"/> in a Razor based test fails.
88
/// </summary>
99
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Do not need them")]
1010
public class RazorTestFailedException : Exception

0 commit comments

Comments
 (0)