Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception System.InvalidCastException thrown when running NUnit and FsCheck tests #855

Closed
sideeffffect opened this issue Jul 7, 2015 · 9 comments

Comments

@sideeffffect
Copy link

Hello, I have a trivial project started from ProjectScaffold.
I have a test file tests/FSharpHello.Tests/Tests.fs like this:

module FSharpHello.Tests

open FSharpHello
open NUnit.Framework
open FsUnit
open FsCheck
open FsCheck.NUnit

[<Test>]
let ``hello returns 42`` () =
  Library.hello 42 |> should equal 42

[<Property>]
let ``hello alwaysreturns 42``(x:int) =
  Library.hello x = 42

and I get error like this

mono  /home/ondra/Projects/FSharpHello/packages/NUnit.Runners/tools/nunit-console.exe "-nologo" "-noshadow" "-labels" "/home/ondra/Projects/FSharpHello/tests/FSharpHello.Tests/bin/Release/FSharpHello.Tests.dll" "-xml:TestResults.xml" 
WARNING: The runtime version supported by this application is unavailable.
Using default runtime: v4.0.30319
ProcessModel: Default    DomainUsage: Single
Execution Runtime: mono-4.0
Unhandled Exception:
System.InvalidCastException: Cannot cast from source type to destination type.

Server stack trace: 
  at (wrapper xdomain-dispatch) NUnit.Core.RemoteTestRunner:Load (object,byte[]&,byte[]&)

Exception rethrown at [0]: 

  at (wrapper xdomain-invoke) NUnit.Core.RemoteTestRunner:Load (NUnit.Core.TestPackage)
  at NUnit.Util.TestDomain.Load (NUnit.Core.TestPackage package) [0x00000] in <filename unknown>:0 
Running build failed.
Error:
Fake.UnitTestCommon+FailedTestsException: NUnit test failed (156).
  at Fake.NUnitSequential.NUnit (Microsoft.FSharp.Core.FSharpFunc`2 setParams, IEnumerable`1 assemblies) [0x00000] in <filename unknown>:0 
  at FSI_0001.Build+clo@137-11.Invoke (Microsoft.FSharp.Core.Unit _arg7) [0x00000] in <filename unknown>:0 
  at Fake.TargetHelper+targetFromTemplate@155[Microsoft.FSharp.Core.Unit].Invoke (Microsoft.FSharp.Core.Unit unitVar0) [0x00000] in <filename unknown>:0 
  at Fake.TargetHelper.runSingleTarget (Fake.TargetTemplate`1 target) [0x00000] in <filename unknown>:0 

this exception is thrown only when I run the build script ./build.sh from shell. when build or run tests in MonoDevelop, everything seems ok.

is this a bug in FAKE? if not, where is the problem?

@TheAngryByrd
Copy link
Contributor

This issue was also reported on FsCheck:
fscheck/FsCheck#115

A possible work around is to specify -domain:None flag to the nunit console runner, however FAKE doesn't seem to give you an option.

https://github.com/fsharp/FAKE/blob/master/src/app/FakeLib/UnitTest/NUnit/Common.fs

type NUnitDomainModel = 
    /// No domain is created - the tests are run in the primary domain. This normally requires copying the NUnit assemblies into the same directory as your tests.
    | DefaultDomainModel
    /// A test domain is created - this is how NUnit worked prior to version 2.4
    | SingleDomainModel
    /// A separate test domain is created for each assembly
    | MultipleDomainModel with
    member x.ParamString =
        match x with
        | DefaultDomainModel -> ""
        | SingleDomainModel -> "Single"
        | MultipleDomainModel -> "Multiple"

DefaultDomainModel claims to specify "No domain", but doesn't specify None in the ParamString method which seems to default to Single.

I think Default should specify "None" as per Nunit docs but probably needs to Default to single as to not break things.

TheAngryByrd added a commit to TheAngryByrd/FAKE that referenced this issue Jul 24, 2015
forki added a commit that referenced this issue Jul 25, 2015
@sideeffffect
Copy link
Author

this bug is still present with FAKE (4.0.2)

you can see the log from travis

@TheAngryByrd
Copy link
Contributor

@sideeffffect Make sure in your build.fsx you set the domain to DefaultDomainModel to specify no domain.

@sideeffffect
Copy link
Author

I get the error

error FS1129: The type 'NUnitParams' does not contain a field 'DefaultDomainModel'

@TheAngryByrd
Copy link
Contributor

Whoops sorry, Copy paste is not my friend.

Target "RunTests" (fun _ ->
    !! testAssemblies
    |> NUnit (fun p ->
        { p with
            DisableShadowCopy = true
            Domain = NUnitDomainModel.DefaultDomainModel 
            TimeOut = TimeSpan.FromMinutes 20.
            OutputFile = "TestResults.xml" })
)

@sideeffffect
Copy link
Author

great, that worked, seems to be fixed now. thank you!

@DavidTheBugWriter
Copy link

Not fixed for me. My build.fsx has:

Target "RunTests" (fun _ ->
    !! testAssemblies

    |> NUnit (fun p ->

        { p with

            DisableShadowCopy = true

            Domain = NUnitDomainModel.NoDomainModel 

            TimeOut = TimeSpan.FromMinutes 20.

            OutputFile = "TestResults.xml" })
)

and my tests are:

open NUnit.Framework
open FsCheck.NUnit
[< Property>]
let ``Reverse of reverse of a list is the original list ``(xs:list<int>) =
                    List.rev(List.rev xs) = xs
[< Test>]
let ``hello returns 42`` () =
  let result = Library.hello 42
  printfn "%i" result
  Assert.AreEqual(42,result)`

Still gives me the error

Tests Not Run:

  1. NotRunnable : secondproject.Tests.Reverse of reverse of a list is the original list
    Test method has non-void return type, but no result is expected

FAKE version is 4.17.1

@rprouse
Copy link

rprouse commented Jun 23, 2016

Are changes required for this from the NUnit team? If so, please report to nunit/nunit#731

@matthid
Copy link
Member

matthid commented May 6, 2017

@DavidTheBugWriter Feel free to open a new issue if needed.

@matthid matthid closed this as completed May 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants