我正在寻找能够利用该语言的独特功能的框架.我知道FsUnit.你会推荐别的吗?为什么?
我试图找出如何使用FsUnit正确测试异常.官方文档指出,要测试异常,我必须这样:
(fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
Run Code Online (Sandbox Code Playgroud)
但是,如果我没有用[<ExpectedException>]属性标记我的测试方法,它将始终失败.听起来很合理,因为如果我们想测试异常,我们必须在C#+ NUnit中添加这样的属性.
但是,只要我添加了这个属性,我试图抛出什么样的异常并不重要,它将始终处理.
一些片段:我的LogicModule.fs
exception EmptyStringException of string
let getNumber str =
if str = "" then raise (EmptyStringException("Can not extract number from empty string"))
else int str
Run Code Online (Sandbox Code Playgroud)
我的LogicModuleTest.fs
[<Test>]
[<ExpectedException>]
let``check exception``()=
(getNumber "") |> should throw typeof<LogicModule.EmptyStringException>
Run Code Online (Sandbox Code Playgroud) 我想检查一个值是否是受歧视联合的特定情况,而不必检查任何包含的数据.我的动机是每个单元测试只测试一件事.
示例如下(最后两行给出编译错误):
module MyState
open NUnit.Framework
open FsUnit
type MyState =
| StateOne of int
| StateTwo of int
let increment state =
match state with
| StateOne n when n = 10 -> StateTwo 0
| StateOne n -> StateOne (n + 1)
| StateTwo n -> StateTwo (n + 1)
[<Test>]
let ``incrementing StateOne 10 produces a StateTwo`` ()=
let state = StateOne 10
(increment state) |> should equal (StateTwo 0) // works fine
(increment state) |> should …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个可以运行FsUnit的基本FAKE F#项目,但我无法弄清楚如何解决这些Method not found: 'Void FsUnit.TopLevelOperators.should(Microsoft.FSharp.Core.FSharpFunc`2<!!0,!!1>, !!0, System.Object)'错误.
我已经阅读了以下似乎相关的帖子,但我显然仍然不喜欢它:
我已经JunkTest使用以下设置创建了一个库项目:
source https://www.nuget.org/api/v2
nuget FAKE
nuget FSharp.Core
nuget FsUnit
nuget NUnit
nuget NUnit.Console
Run Code Online (Sandbox Code Playgroud)
FSharp.Core
FsUnit
NUnit
Run Code Online (Sandbox Code Playgroud)
module JunkTest
open FsUnit
open NUnit.Framework
[<Test>]
let ``Example Test`` () =
1 |> should equal 1 // this does not work
//Assert.That(1, Is.EqualTo(1)) // this works (NUnit)
Run Code Online (Sandbox Code Playgroud)
Target "Test" (fun _ ->
!! (buildDir + "JunkTest.dll")
|> NUnit3 (fun p ->
{p …Run Code Online (Sandbox Code Playgroud) 我是F#的新手,我正在玩FParsec.我会使用FParsec生成AST.我想使用FsUnit围绕解析器的各个部分编写一些测试,以确保正确的操作.
我在语法上遇到了一些麻烦(对不起,确切的代码在起作用,我可以稍后发布一个具体的例子)那么如何比较两个有区别的联合(一个是预期的,另一个是实际的结果)?有人可以使用FsUnit(或NUnit)提供一个很小的代码示例吗?
一个受歧视的联合示例(非常简单)
type AST =
| Variable of string
| Class of string
| Number of int
Run Code Online (Sandbox Code Playgroud) 出于某种原因,当通过TestCaseattrubute 将参数传递给测试时,我得到以下关于第一个参数的错误消息,在本例中是一个数组:
这不是有效的常量表达式或自定义属性值
module GameLogicTest =
open FsUnit
open NUnit.Framework
open GameLogic.Examle
// This is not a valid constant expression or custom attribute value
[<TestCase( [| 1; 2; 3 |], 3, 1,1)>]
let ``let example.`` (a, m, h, c) =
a
|> proof1 m
|> should equal (h,c)
Run Code Online (Sandbox Code Playgroud)
但是当从属性和方法本身中删除最后一个参数时,一切正常.
[<TestCase( [| 1; 2; 3 |], 3, 1)>]
let ``let example.`` (a, m, h) =
a
|> proof1 m
|> should equal (h,1)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我最好也定义一个元组int * int但它似乎也不起作用.
f# custom-attributes constant-expression fsunit parameterized-tests
我正在使用FsUnit 2.3.2,但对失败消息不满意。请参阅以下示例:
[<Test>]
let ``test 1``() =
[1; 3]
|> should equal [1;2]
Run Code Online (Sandbox Code Playgroud)
...给了我不太有用的信息:
预期的和实际的都是Microsoft.FSharp.Collections.FSharpList`1 [System.Int32]
在d:\ GitHub \ FsUnit \ src \ FsUnit.NUnit \ FsUnit.fs:第44行位于d:\ GitHub \ FsUnit \ src \ FsUnit.TopLevelOperators.F.Program.test 1()中F:\ work \操场\ fsunit \ fsunit \ Program.fs:第9行
我发现一种解决方法是使用数组而不是列表:
[<Test>]
let ``test 2``() =
[|1; 4|]
|> should equal [|1;2|]
Run Code Online (Sandbox Code Playgroud)
...产生
预期和实际都是System.Int32 [2]
值在索引[1]处不同
预期:2
但是是:4
第二个问题是我是否定义了ADT
type MyT =
A of int
| B of string
[<Test>]
let ``test 4``() =
A 10
|> should equal …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查我的函数是否返回 Some(x)
testedFunc() |> should be (sameAs Some)
testedFunc() |> should be Some
testedFunc() |> should equal Some
Run Code Online (Sandbox Code Playgroud)
都行不通。我宁愿不使用:
match testedFunc() with
| Some -> Pass()
| None -> Fail()
Run Code Online (Sandbox Code Playgroud)
有人知道这样做的方法吗?
我正在使用FsUnit 2.1(使用NUnit 3.2)为F#项目编写测试.这是一个简单的模块:
namespace Library1
module LibraryFunctions =
let Execute f1 = f1()
let Id x = x
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
namespace Tests
open FsUnit
open NUnit.Framework
open Library1
[<TestFixture>]
type Tests() =
[<Test>]
// Passes
member x.``LibraryFunctions.Id should return the value``() =
LibraryFunctions.Id 42 |> should equal 42
[<Test>]
// Fails
member x.``LibraryFunctions.Execute should return the result of the function``() =
let f() = 42
LibraryFunctions.Execute f |> should equal 42
Run Code Online (Sandbox Code Playgroud)
第二次测试失败(在NCrunch和ReSharper中),并显示以下消息:
System.MissingMethodException : Method not found: '!!0 Library1.LibraryFunctions.Execute(Microsoft.FSharp.Core.FSharpFunc`2<Microsoft.FSharp.Core.Unit,!!0>)'.
如果我将测试模块放在与测试相同的代码文件中(而不是在单独的VS项目中),则测试通过.我怀疑这是由于NUnit和F#/ C#interop的一些问题.如果是这样,怎么解决?
我正在尝试使用 FsUnit.Xunit 断言某种异常类型和消息。FsUnit附带了一些throwWithMessage功能。但是,当使用它代替throw函数 fsc 时,会发出以下错误:
C:\src\foo.fs(29,12): error FS0001: This expression was expected to have type '(unit -> unit) -> 'a' but here has type 'unit'
C:\src\foo.fs(29,19): error FS0001: The type ''a -> NHamcrest.Core.CustomMatcher<obj>' is not compatible with the type 'NHamcrest.IMatcher<obj>'
C:\src\foo.fs(29,12): error FS0001: This expression was expected to have type '(unit -> unit) -> 'a' but here has type 'unit'
Run Code Online (Sandbox Code Playgroud)
这是无法编译的测试:
[<Fact>]
let ``Some test`` () =
(fun () -> This.Throws("a", 10) |> ignore)
|> should …Run Code Online (Sandbox Code Playgroud)