可以将xUnit与LINQPad一起使用吗?

时间:2018-10-14 00:21:21

标签: xunit linqpad xunit.net

是否可以将xUnit与LINQPad一起使用?

能够为LINQPad首先设计的概念编写一些测试将是很棒的。它比添加另一个ConsoleApp23423894238容易得多,只是能够快速编写一些单元测试。

4 个答案:

答案 0 :(得分:4)

是的,可以将xUnit与LinqPad一起使用。我已经更新了此答案以为您提供最新信息,但是我给出的原始答案还为您提供了有关xUnit使用的一些见解和链接,因此我将其保留以供您参考。


更新 LinqPad 6的最新版本(自6.9.x及更高版本起,请参见6.9 Release Notes section现在具有内置的XUnit支持(通过新菜单项Query / Add XUnit Test Support)。这将添加带有所需样板代码referenced via #load的查询,以便您可以立即开始测试事实和理论。
您可以通过按 Alt + Shift + T 来运行示例测试方法。

例如,将以下新理论添加到样板代码中(在main方法之后,将其放在private::Tests区域内):

#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

Alt + Shift + T 并检查结果。


现在,如果您愿意,请继续阅读原始答案(最后有一些有用的提示):


两个答案将我引向正确的方向,但是描述还不够完整,因此我不得不做一些实验。为了节省您的时间,我在描述我的所作所为。我现在有适用于 LinqPad 5和LinqPad 6的解决方案。

LinqPad 5-用于.NET

基于汤姆的所作所为,我得以使其与 LinqPad 5(v5.42.01)一起使用。

还需要执行一些其他步骤,这些步骤并不明显,也没有在接受的答案中进行解释,因此,我将说明对我有帮助的步骤。

首先,您将需要使用LinqPad的付费版本,因为只有这样,您才能加载NUGET软件包。

在LinqPad中加载所需的程序包,请按 F4 。查询属性对话框打开。在该对话框中,单击添加NUGET ...

NUGET对话框打开。在NUGET对话框的中间,有一个搜索文本框。搜索xUnit.Net-显示后,单击添加到查询,等待其加载,然后单击添加名称空间。。然后对{{ 1}}

然后关闭NUGET对话框,但保持查询属性打开。您需要转到高级标签:启用(勾号)“将所有非框架参考复制到单个本地文件夹中。” 现在,您可以关闭< strong>查询属性对话框。

将以下示例导入查询编辑器(Xunit.Runner.LinqPad.模式):

C# Program

运行它,您将看到测试运行程序的输出。

正在运行3个测试中的3个...
已完成:在0,003秒内进行了3次测试(0次失败,0次跳过)

通过将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 } } 中的assert更改为Test_Fact(),然后再次运行它,来修改测试以查看测试失败的外观。它应该显示这次:

正在运行3个测试中的3个...
[FAIL] UserQuery + A_XUnitSelfTest.Test_Fact:Assert.False()失败
预期:错误
实际:真实
在Xunit.Assert.False(可为空,条件为1,字符串userMessage)
在Xunit.Assert.False(布尔条件)时
在C:\ Users ... \ AppData \ Local \ Temp \ LINQPad5_oyxxqxbu \ query_mxsmky.cs中的UserQuery.A_XUnitSelfTest.Test_Fact()中:第62行
已完成:在0,005秒内进行了3次测试(1次失败,0次跳过)


LinqPad 6-用于.NET Core

LinqPad 6有所不同,因为它基于.NET Core,因此确实需要不同的库。

从此开始:

Assert.False(1 == 1);

然后通过 F4 对话框添加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 } } xUnit.netxUnit.net [Core Unit Testing Framework]及其所有名称空间(详细信息请参阅LinqPad 5会话),最后复制XUnit运行器源代码as mentioned by @ramiroalvarez至上述xUnit测试示例的末尾:

XUnit runner source code

在F4对话框的“高级”选项卡中启用“将所有nuget程序集复制到单个本地文件夹” ...应该足够了,但是请不要继续阅读

如果尝试使用它,则会在源代码中引发异常InvalidOperationException(“请为无框架引用(F4->高级)启用影子文件夹选项。”)。

现在,我查看了 XUnit运行器,发现如果注释掉两行代码,则它运行良好:

原始代码(位于方法xUnit.net [Runner Utility] 中):

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);

您就完成了! (已使用LinqPad 6 v6.8.3版本,X64版本进行测试)


提示:

提示1:
您可以按照说明将xUnit运行程序附加到代码中,但是LinqPad 6中有一种更优雅的方法: 将一个仅包含xUnit.Runner源代码的副本保存在与单元测试文件相同的路径中,其名称为// ... // throw new InvalidOperationException("Please enable the shadow folder option ..."); // ... // File.Copy(assemblyFilename, targetAssembly, true); 。 然后,将以下内容添加到测试代码的顶部:
LinqPadRunner

现在您已经清理了代码!每次创建新测试时,只需记住添加xUnit.Runner.LinqPad6.linq命令。

提示2:
将以下2个属性添加到测试运行程序的代码中:

#load

如果您要跳过测试,这很有用。要跳过测试,只需将/// <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"; } } 替换为[Fact]或将[SkipFact]替换为[Theory]

如果您正忙于编写测试,并且只想测试单个测试,则将所有属性设置为[SkipTheory]或同样设置为[SkipFact],除了您正在编写的单个测试。

然后,要不时快速地再次运行所有测试-无需更改每个属性-将以下内容添加到测试套件的初始化部分(请参见global setup for startup and teardown):

[SkipTheory]

完成后,还原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 } } / [Fact]属性,然后将[Theory]变量设置回.OverrideSkip


有关单元测试的更多信息

有关使用xUnit进行单元测试的一些高级主题,我建议阅读:

如果要在Visual Studio中使用它,则可以找到信息here

答案 1 :(得分:2)

可能吗?有点。

这既不是LINQPad的主要用例,也不是单元测试的用例。

关于LINQPad,您可以使用现有的测试运行器,但是可能与LINQPad集成的唯一测试器是控制台运行器。测试运行程序不是一个简单的程序。尝试Xunit.Runner.LinqPad

关于需要“为正在设计的概念编写一些测试”,请考虑,如果您发现单元测试工作流程没有足够的价值来创建项目并将其保留在源代码管理中,则可能无法充分利用它们。获得更多价值的一种方法是使用行为驱动开发(Behavior-Driven Development,BDD),如果您想以商业可读的语言编写需求,请尝试使用SpecFlow之类的方法。

答案 2 :(得分:1)

感谢asherber,xunit现在可以与linqpad6(netcore)一起使用了。

https://github.com/asherber/Xunit.Runner.LinqPad/blob/master/Xunit.Runner.LinqPad/XunitRunner.cs

在查询属性中非常重要,请选中“将所有nuget程序集复制到单个本地文件夹”,否则,将出现以下错误“ InvalidOperationException:请为无框架引用启用阴影文件夹选项”

Linqpad6 + Xunit

答案 3 :(得分:0)

我只是试图打开一个LINQPad查询文件,该文件试图加载xunit package,但出现了一个错误,多年来我一直没有发生过使用LINQPad的错误。所以我的回答是

相关问题