通常我使用F#编写我的单元测试
open Swensen.Unquote open Xunit module MyTests = [<Fact>] let ``SomeFunction should return 10`` () = let a = SomeFunction() test <@ a = 10 @> [<Fact>] let ``SomeOtherFunction should return 11`` () = let a = SomeFunction() test <@ a = 11 @>如果我想从xunit登录到控制台(根据http://xunit.github.io/docs/capturing-output.html ),需要编写一个构造函数,它需要一个ITestOutputHelper ,然后使用它来代替Console.WriteLine和家人。
using Xunit; using Xunit.Abstractions; public class MyTestClass { private readonly ITestOutputHelper output; public MyTestClass(ITestOutputHelper output) { this.output = output; } [Fact] public void MyTest() { var temp = "my class!"; output.WriteLine("This is output from {0}", temp); } }但是fsharp模块是静态类,测试是静态方法。 没有构造函数来注入输出帮助器。
有没有一种方法可以访问当前测试的当前输出帮助器。 我知道我可以重写我的fsharp测试是非静态类,但这是不希望的。
看了XUnit的资料。
https://github.com/xunit/xunit/blob/e64f566b75f93cd3cec27f950759d82832bfe44b/src/xunit.execution/Sdk/Frameworks/Runners/TestClassRunner.cs#L90
我很确定这是一个被忽视的案例。 没有帮助者注入静态类。
Normally I write my unit tests in F# as
open Swensen.Unquote open Xunit module MyTests = [<Fact>] let ``SomeFunction should return 10`` () = let a = SomeFunction() test <@ a = 10 @> [<Fact>] let ``SomeOtherFunction should return 11`` () = let a = SomeFunction() test <@ a = 11 @>If I wish to log to the console from xunit ( according to http://xunit.github.io/docs/capturing-output.html ) one needs to write a constructor that takes an ITestOutputHelper and then use that instead of Console.WriteLine and family.
using Xunit; using Xunit.Abstractions; public class MyTestClass { private readonly ITestOutputHelper output; public MyTestClass(ITestOutputHelper output) { this.output = output; } [Fact] public void MyTest() { var temp = "my class!"; output.WriteLine("This is output from {0}", temp); } }however fsharp modules are static classes and the tests are static methods. There is no constructor to inject the output helper.
Is there a way to get access to the current output helper for the current test. I know I could rewrite my fsharp tests to be non static classes but that is undesired.
After looking at the XUnit source.
https://github.com/xunit/xunit/blob/e64f566b75f93cd3cec27f950759d82832bfe44b/src/xunit.execution/Sdk/Frameworks/Runners/TestClassRunner.cs#L90
I'm pretty sure this is an overlooked case. There is no injection of the helper into static classes.
最满意答案
如果xUnit没有用于注入参数的其他机制,那么我猜测唯一的选择是将测试定义为F#对象类型中的方法。 我也更喜欢使用let编写测试作为函数,但下面的简单对象类型看起来不太糟糕:
open Swensen.Unquote open Xunit open Xunit.Abstractions type MyTests(output:ITestOutputHelper) = [<Fact>] member __.``SomeFunction should return 10`` () = let a = SomeFunction() output.WriteLine("Some function returned {0}", a) test <@ a = 10 @>如果xUnit支持其他一些选项 - 我怀疑它们可能会对建议持开放态度,如果它不会太尴尬(可能使用方法参数?)
但除非xUnit增加对其他方法的支持,否则我认为您需要将F#对象与方法一起使用。
If xUnit does not have any alternative mechanism for injection of the parameters, then I guess the only option is to define the tests as methods in an F# object type. I also prefer writing tests as functions using let, but the following simple object type does not look too bad:
open Swensen.Unquote open Xunit open Xunit.Abstractions type MyTests(output:ITestOutputHelper) = [<Fact>] member __.``SomeFunction should return 10`` () = let a = SomeFunction() output.WriteLine("Some function returned {0}", a) test <@ a = 10 @>It would be nice if xUnit supported some other option for this - I suspect that they might be open to suggestions, if it is something that would not be too awkward (perhaps using a method parameter?)
But unless xUnit adds support for some other method, I think you'll need to use F# object with methods.
更多推荐
xunit,output,测试,电脑培训,计算机培训,IT培训"/> <meta name="description&q
发布评论