Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use xUnit with LINQPad?

Is it possible to use xUnit with LINQPad?

It would be great to be able to write some tests for concepts being designed in LINQPad first. It's easier than adding yet another ConsoleApp23423894238 just to be able to quickly write some unit tests.

like image 606
kimsagro Avatar asked Dec 07 '22 13:12

kimsagro


2 Answers

Yes, it is possible to use xUnit with LinqPad. I have updated this answer to give you the latest information, but the original answer I gave also gives you some insights and links regarding the usage of xUnit, so I kept it for your reference.


UPDATE: Latest version of LinqPad 6 or 7 (since V 6.9.x and higher, see 6.9 Release Notes section) has now built-in XUnit support via the new menu item Query / Add XUnit Test Support. This will add a query with required boilerplate code referenced via #load so you can start testing facts and theories instantly.
You can run the example test method by pressing Alt+Shift+T.

For example, add the following new theory to the boilerplate code (right after the main method, put it inside the private::Tests region):

#region private::Tests

[Theory]
[InlineData(1)]  
[InlineData(3, 6)]  
[InlineData(5, 7, 9)]
void OddNumbers(params int[] values)
{
    static bool IsOdd(int value) => value % 2 == 1;
    foreach (var value in values)
    {
        Assert.True(IsOdd(value), $"IsOdd({value}) is false");  
    } 
}

#endregion

Press Alt+Shift+T to run all tests and check the result.

Note: It also adds a file XUnit.linq to your query directory the first time you add XUnit Test support - this is a mini library to support xUnit tests and you should not touch it unless you know exactly what you're doing. For example, you can modify this file to change the way the test summary is being displayed (for example, you can add the parameter Func<UserQuery.TestResultSummary, bool> filter = null to the method RunTests and add .Where(filter) inside the query if filter is not null. This allows you to filter the results of the test summary).

For example: RunTests(filter: f => f.MethodName.StartsWith("Unit")); will only display the tests in the summary which names start with "Unit".


Dynamically skippable unit tests

You can modify the xunit script LinqPad 6 provides to support skippable unit tests (NUGET package XUnit.SkippableFact, detailed description see the link "dynamically skipping test and theory" at the end of this answer).

Make the following changes in xunit:

  1. function RunTests - add: runner.OnTestSkipped = info => AddTestResult(info);

  2. class TestResultSummary - add: public bool Skipped() => _testInfo is Xunit.Runners.TestSkippedInfo;

  3. class TestResultSummary / public object Status - add: _testInfo is Xunit.Runners.TestSkippedInfo ? Util.WithStyle ("Skipped", "color:orange") :

Those changes will enable the xunit script to show when a test was skipped in the results.

To test it, load the NUGET package XUnit.SkippableFact via F4, then add the test framework and finally add the tests:

[SkippableFact]
void Test_Xunit3()
{

Skip.If(true, &quot;skipping&quot;);

Assert.True(1 + 1 == 2);

}

[SkippableTheory]
[InlineData(1)]
[InlineData(2)]
void Test_Xunit4(int x)
{

Skip.If(x==2, &quot;skipping&quot;);

Assert.True(1 + 1 == 2);

}

Note: Those changes will also work with the plain XUnit. So adding the changes to the code does not harm.


Now if you like, continue reading the original answer (with some useful hints at the end):


Two answers pointed me into the right direction, but the descriptions haven't been complete enough, so I had to experiment a bit. To save you that time, I am describing what I did; I have now working solutions for LinqPad 5 and LinqPad 6 (and 7).

LinqPad 5 - for .NET

Based on what Tom wrote, I was able to get it working with LinqPad 5 (v5.42.01).

There are some more steps required, which aren't obvious and not explained in the accepted answer, so I'll describe what helped on my side.

First of all, you will need to use the paid version of LinqPad, because only then you are able to load NUGET packages.

To load the required packages in LinqPad, press F4. The query properties dialog opens up. In that dialog, click Add NUGET....

The NUGET dialog opens up. In the middle of the NUGET dialog, there is a search textbox. Search for xUnit.Net - once it got displayed, click Add to query, wait until it loaded, then click on Add namespaces. Then do the same for Xunit.Runner.LinqPad.

Then close the NUGET dialog, but keep the query properties open. You need to to there to the Advanced tab: Enable (tick-mark) "Copy all non-framework references to a single local folder." Now you can close the query properties dialog.

Import the following example into the query editor (C# Program mode):

void Main()
{
     // Press F4, go to Advanced, then 
     // tick-mark "Copy all non-framework references to a single local folder"
     var me = Assembly.GetExecutingAssembly();
     var result = Xunit.Runner.LinqPad.XunitRunner.Run(me); 
     result.Dump("Xunit runner result");
}
    
/// <summary>
/// This test class is there for demo purpose, so developers can
/// see how to write test methods. 
/// It also verifies that XUnit itself is functioning correctly.
/// </summary>
public class A_XUnitSelfTest
{

    [InlineData(2)]
    [InlineData(3)]
    [Theory]
    public void Test_Theory(int a)
    {
        Assert.True(a == 2 || a == 3); // succeeds
    }

    [Fact]
    public void Test_Fact()
    {
        Assert.False(1 == 0); // succeeds
    }

}

Run it and you will see the output of the test runner.

Running 3 of 3 tests...
Finished: 3 tests in 0,003s (0 failed, 0 skipped)

Modify the test to see how it looks like if a test fails by changing the assert in the Test_Fact() to Assert.False(1 == 1);, then run it again. It should show this time:

Running 3 of 3 tests...
[FAIL] UserQuery+A_XUnitSelfTest.Test_Fact: Assert.False() Failure
Expected: False
Actual: True
at Xunit.Assert.False(Nullable`1 condition, String userMessage)
at Xunit.Assert.False(Boolean condition)
at UserQuery.A_XUnitSelfTest.Test_Fact() in C:\Users...\AppData\Local\Temp\LINQPad5_oyxxqxbu\query_mxsmky.cs:line 62
Finished: 3 tests in 0,005s (1 failed, 0 skipped)


LinqPad 6 - for .NET Core

LinqPad 6 is different, because it is based on .NET Core and hence does require different libraries.

Start with this:

void Main()
{
     // Press F4, go to Advanced, then 
     // tick-mark "Copy all non-framework references to a single local folder"
     var me = Assembly.GetExecutingAssembly();
     var result = Xunit.Runner.LinqPad.XunitRunner.Run(me); 
     result.Dump("Xunit runner result");
}
    
/// <summary>
/// This test class is there for demo purpose, so developers can
/// see how to write test methods. 
/// It also verifies that XUnit itself is functioning correctly.
/// </summary>
public class A_XUnitSelfTest
{

    [InlineData(2)]
    [InlineData(3)]
    [Theory]
    public void Test_Theory(int a)
    {
        Assert.True(a == 2 || a == 3); // succeeds
    }

    [Fact]
    public void Test_Fact()
    {
        Assert.False(1 == 0); // succeeds
    }

}

Then add xUnit.net, xUnit.net [Core Unit Testing Framework] and xUnit.net [Runner Utility] and all their namespaces via the F4 dialog (details see LinqPad 5 sesion) and finally copy the XUnit runner source code as mentioned by @ramiroalvarez to the end of the xUnit test example above:

XUnit runner source code

Enable "copy all nuget assemblies to a single local folder" in the F4 dialog, Advanced tab ... that should be enough, but it isn't so read on:

If you try it, it throws the exception InvalidOperationException("Please enable the shadow folder option for none-framework references (F4 -> Advanced).") in the source code.

Now I looked into the XUnit runner, and found that if you comment out 2 lines of code then it is running fine:

Original code (located in method GetTargetAssemblyFilename):

if (shadowFolder != xunitFolder 
    || Directory.GetFiles(shadowFolder, "xunit.execution.*.dll").Length == 0)
{
    throw new InvalidOperationException("Please enable the shadow folder option ...");
}

// ...

var targetAssembly = Path.Combine(shadowFolder, Path.GetFileName(assemblyFilename));
//Console.WriteLine($"Copy \"{ assemblyFilename }\" -> \"{ targetAssembly }\"");
File.Copy(assemblyFilename, targetAssembly, true);

Comment the lines

// ...
// throw new InvalidOperationException("Please enable the shadow folder option ...");
// ...
// File.Copy(assemblyFilename, targetAssembly, true);

and you're done! (Tested with v6.8.3 version of LinqPad 6, X64 version)


Hints:

Hint 1:
You can append the xUnit runner to your code as described, but there is a more elegant way in LinqPad 6: Save a copy containing only the xUnit.Runner source code in the same path as your unit test files with the file name xUnit.Runner.LinqPad6.linq. Then, add the following to the top of your test code:
LinqPadRunner

Now you have cleaned up your code! Every time you create a new test, just remember to add the #load command.

Hint 2:
To be able to skip facts statically, add the following 2 attributes to the code of the test runner:

/// <summary>
/// Skip a Fact (you can override it by setting OverrideSkip=true)
/// </summary>
public class SkipFactAttribute : FactAttribute
{
    /// <summary>
    /// Override SkipFact: Set OverrideSkip=true to run even skipped tests
    /// </summary>
    public static bool OverrideSkip = false; // set to true to ignore skip

    /// <summary>
    /// Constructor. Used to set skip condition.
    /// </summary>
    public SkipFactAttribute()
    {
        if (!OverrideSkip) Skip = "Skipped Fact";
    }
}

/// <summary>
/// Skip a Theory (you can override it by setting OverrideSkip=true)
/// </summary>
public class SkipTheoryAttribute : TheoryAttribute
{
    /// <summary>
    /// Override SkipTheory: Set OverrideSkip=true to run even skipped tests
    /// </summary>
    public static bool OverrideSkip = false; // set to true to ignore skip

    /// <summary>
    /// Constructor. Used to set skip condition.
    /// </summary>
    public SkipTheoryAttribute()
    {
        if (!OverrideSkip) Skip = "Skipped Theory";
    }
}

This is useful, if you want to skip tests. To skip a test, simply replace [Fact] by [SkipFact] or [Theory] by [SkipTheory].

If you're busy writing a test and you want to test only that single test, set all attributes to [SkipFact] or likewise [SkipTheory], except the single one you're writing currenlty.

Then, to quickly run all the tests again from time to time - without having to change each and every single attribute - add the following to the initialization part of your test suite (see global setup for startup and teardown):

public class TestsFixture : IDisposable
{

    public TestsFixture() // running once before the tests
    {
        // true: Ignore Skip and run all tests, false: Skip is effective
        SkipFactAttribute.OverrideSkip = true;
        SkipTheoryAttribute.OverrideSkip = true;
    }

    public void Dispose() // running once, after the tests
    {
         // teardown code
    }
}

If you're done, restore the [Fact]/ [Theory] attributes and set the .OverrideSkip variables back to false.

NOTE: In case you want to skip facts based on a runtime condition, take a look at dynamically skipping test and theory.


More about unit testing

For some advanced topics about unit testing with xUnit, I recommend to read:

  • xunit tests in net core
  • global setup for startup and teardown
  • shared context
  • running tests in parallel
  • running tests in prioritized, specified order
  • configuring with json
  • configuring with xml
  • skipping Test and Theory // description works for TheoryAttribute too
  • dynamically skipping test and theory
  • xUnit Theory: Working With InlineData, MemberData, ClassData

If you want to use it with Visual Studio, you can find infos here.

like image 102
Matt Avatar answered Dec 10 '22 02:12

Matt


Possible? Sort of.

This is neither a main use case of LINQPad nor of unit testing.

Regarding LINQPad, you could use an existing test runner but probably the only ones that could be integrated with LINQPad are console runners. A test runner is not a trivial program. Try Xunit.Runner.LinqPad.

As for needing to "write some tests for concepts being designed", consider that you might not be getting enough out of your unit testing workflow if you don't find them valuable enough to create a project and keep it in source control. One way to get more value is to use Behavior-Driven Development (BDD) and if you want to write the requirements in a business-readable language, try something like SpecFlow.

like image 26
Tom Blodget Avatar answered Dec 10 '22 01:12

Tom Blodget