Build/Test Issues

bUnit + xUnit Razor tests failing

Started by ClaytonHunt on 3,127 views

I am testing a Blazor application. I have normal C# tests in this project that run just fine, however, when adding this first Razor syntax test I get the following error.

All other tests work, and This test runs in VS Test runner, and in Resharper Test Runner. I have tried to provide enough information for reproduction of the issue. Sorry if it is too much.
I absolutely love NCrunch and have been a user for nearly 10 years at this point. Thank you for all the hard work and I appreciate any help I can get for this issue.

My version of NCrunch is 4.6.0.3 and is running in Visual Studio Pro 2019 Version 16.8.5


An error occurred while analysing this project after it was built: System.Exception: DiscoverRazorTests: Discovering in MyProject.MyTests.
GetSourceInformation(MyTests): Attempting to find source file
GetSourceInformation(MyTests): Verifying file = C:\{MyPath}\MyTests.razor
GetSourceInformation(MyTests): Source info found: File = C:\{MyPath}\MyTests.razor, LineNumber = 3
Error while discovering test 'MyProject.MyTests.RazorTests':System.NullReferenceException: Object reference not set to an instance of an object.
   at nCrunch.Module.XUnit2.Integration.XUnitNCrunchDiscoveredTestContainer.convertTraitsToCategories(Dictionary`2 traits)
   at nCrunch.Module.XUnit2.Integration.XUnitNCrunchDiscoveredTestContainer.StoreDiscoveredTest(ITestCase testCase, TestName testName)
   at nCrunch.Module.XUnit2.Integration.XUnitDiscoveryMessageSink.discoverTest(ITestCase testCase)
   at nCrunch.Module.XUnit2.Integration.XUnit2DiscoveryEnvironment.FindFrameworkTestsInAssembly(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, ComponentUniqueName testComponentUniqueName, PlatformType platformType, DynamicProxy[] dynamicProxies)
   at nCrunch.TestExecution.TestFinder.<>c__DisplayClass0_2.<FindTestsForFrameworks>b__1()
   at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
   at nCrunch.TestExecution.TestFinder.<>c__DisplayClass0_0.<FindTestsForFrameworks>b__0()
   at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
   at nCrunch.TestExecution.TestFinder.FindTestsForFrameworks(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, DescribedTestFrameworkDiscoverer[] describedDiscoverers, ComponentUniqueName testComponentUniqueName, PlatformType platformType, DynamicProxy[] dynamicProxies)
   at nCrunch.TestExecution.RemoteTaskRunner.AnalyseAssembly(DescribedTestFrameworkDiscoverer[] applicableFrameworks, ComponentUniqueName testComponentUniqueName, PerfTracker perfTracker)


MyProject.csproj

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <IsPackable>false</IsPackable>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="bunit" Version="1.0.0-preview-01" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />

    <PackageReference Include="coverlet.collector" Version="3.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>  

</Project>


_Imports.razor

@using Xunit
@using Bunit
@using AngleSharp.Dom
@using Bunit.TestDoubles
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.Extensions.DependencyInjection


MyTests.razor

@inherits TestComponentBase

<Fixture Test="NoErrorsDisplaysEmpty">
    <ComponentUnderTest>
        <h1>Hello World</h1>
    </ComponentUnderTest>

    @code {
        public void NoErrorsDisplaysEmpty(Fixture fixture)
        {

        }
    }
</Fixture>
Hi, thanks for sharing this issue.

This problem is caused by a compatibility issue between NCrunch and bunit. Basically, bunit changes some of the behaviour around how the test run is handled and the results are reported. Presently, we do not have support for this framework. Adding support would need to go through a proper development process so that we can test the use cases and patch up any issues. You are most welcome to make a feature request for it if you like.
Remco wrote:Hi, thanks for sharing this issue.

This problem is caused by a compatibility issue between NCrunch and bunit. Basically, bunit changes some of the behaviour around how the test run is handled and the results are reported. Presently, we do not have support for this framework. Adding support would need to go through a proper development process so that we can test the use cases and patch up any issues. You are most welcome to make a feature request for it if you like.


Hi @Remco, creator of bUnit here. Can you elaborate on what I might do differently?

My extension to xUnit to enable writing tests in .razor tests follows the pattern recommend by the xUnit framework, where one extends a bunch of xUnit base classses to create a custom test discoverer and test runner.

@ClaytonHunt have you tried to write tests using the "inline render fragment" available in preview 1 of bUnit. I do not have a NCrunch license, so havent tested my self, but the code looks like this (code picked from here):

@inherits TestContext
@code {
    [Theory, AutoData]
    public void PizzaCard_Correctly_Lists_The_PizzaSpecial_Passed_To_It(PizzaSpecial pizza)
    {
        var cut = Render(@<PizzaCard Special="pizza" />);

        cut.MarkupMatches(@<li style="background-image: url('@pizza.ImageUrl')">
                                <div class="pizza-info">
                                    <span class="title">@pizza.Name</span>
                                    @pizza.Description
                                    <span class="price">@pizza.GetFormattedBasePrice()</span>
                                </div>
                            </li>);
    }
    
    [Theory, AutoData]
    public void PizzaCard_Raises_Clicked_Event_When_Clicked_By_User(PizzaSpecial inputPizza)
    {
        PizzaSpecial? clickedPizza = null;
        var cut = Render(@<PizzaCard Special="inputPizza" OnClick="pizza => clickedPizza = pizza" />);     

        cut.Find("li").Click();

        clickedPizza.Should().Be(inputPizza);
    }
}


It would be interesting to know if this works with NCrunch.

NOTE: Using AutoFixture to generate test data (thats the AutoData attribute).

Best regards, Egil

Edited

Excellent work around! @Egilhansen Initial results for that format of a test are working fine. It must be something in the <Fixture> component that is blowing up. I am unfamiliar with AutoFixture I will have to look into that.
ClaytonHunt wrote:Excellent work around! @Egilhansen Initial results for that format of a test are working fine. It must be something in the <Fixture> component that is blowing up. I am unfamiliar with AutoFixture I will have to look into that.


I am using AutoFixture and this style of bUnit tests on stream these days. Talk about AutoFixture in details during the first episode if you are curious.

Edited

Post a reply

Log in to reply.