将已区分的并集传递给InlineData属性

编程入门 行业动态 更新时间:2024-10-24 07:32:19
本文介绍了将已区分的并集传递给InlineData属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试对解析字符串的语法分析器进行单元测试,并返回相应的抽象语法树(表示为有区别的并集).我认为使用Xunit.Extensions的属性InlineData可以将所有测试用例相互堆叠会很紧凑:

I am trying to unit test a parser that parses a string and returns the corresponding abstract syntax tree (represented as a discriminated union). I figured it would be pretty compact to use Xunit.Extensions' attribute InlineData to stack all test cases on one another:

[<Theory>] [<InlineData("1 +1 ", Binary(Literal(Number(1.0)), Add, Literal(Number(1.0))))>] ... let ``parsed string matches the expected result`` () =

但是,编译器抱怨第二个参数不是文字(如果我正确理解的话,请编译时间常数).

However, compiler complains that the second argument is not a literal (compile time constant if I understand it correctly).

是否有解决方法?如果没有,那么将解析器结果测试结构化同时将每个案例作为单独的单元测试进行保存的最明智的方法是什么?

Is there a workaround for this? If not, what would be the most sensible way to structure parser result tests while keeping every case as a separate unit test?

推荐答案

一种可能性是使用xUnit的MemberData属性.这种方法的一个缺点是,此参数化测试在Visual Studio的测试资源管理器"中显示为一个测试,而不是两个单独的测试,因为集合缺少xUnit的IXunitSerializable接口,并且xUnit也没有为该类型添加内置的序列化支持.有关更多信息,请参见 xunit/xunit/issues/429 .

One possibility is to use xUnit's MemberData attribute. A disadvantage with this approach is that this parameterized test appears in Visual Studio's Test Explorer as one test instead of two separate tests because collections lack xUnit's IXunitSerializable interface and xUnit hasn't added build-in serialization support for that type either. See xunit/xunit/issues/429 for more information.

这是一个最小的工作示例.

Here is a minimal working example.

module TestModule open Xunit type DU = A | B | C type TestType () = static member TestProperty with get() : obj[] list = [ [| A; "a" |] [| B; "b" |] ] [<Theory>] [<MemberData("TestProperty")>] member __.TestMethod (a:DU) (b:string) = Assert.Equal(A, a)

另请参见类似的问题中,我给出了相似的答案.

See also this similar question in which I give a similar answer.

更多推荐

将已区分的并集传递给InlineData属性

本文发布于:2023-11-08 07:51:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   将已   InlineData

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!