System.ComponentModel.Win32Exception when executing F# Nunit unit test code with Mono

会有一股神秘感。 提交于 2019-12-11 13:05:52

问题


I'm trying to use F# unittest on Mono. I use Mac OS X. I downloaded and copied NUNit frameworks.

I have the following environments

nunitFramework=.../bin/mono/NUnit.2.6.4/nunit.framework.dll
console=.../bin/mono/NUnit.2.6.4/nunit-console.exe
fsUnit=.../bin/mono/NUnit.2.6.4/FsUnit.NUnit.dll

This is F# code:

namespace HelloWorld.Core
module Hello = let SayHello name = "Hello"

This is the unittest for it.

module HelloWorld.Tests.Hello  
open HelloWorld.Core.Hello
open NUnit.Framework
open FsUnit

[<Test>]
let shouldSayHello () = Assert.AreEqual("Hello World!", SayHello "World")

I compile the code and unittest:

fsharpc --target:library HelloWorld.fs
fsharpc --target:library -r:HelloWorld.dll -r:$nunitFramework -r:$fsUnit HelloWorldTest.fs

I run the test: mono $console HelloWorldTest.dll to get the error messages.

ProcessModel: Default    DomainUsage: Single
Execution Runtime: mono-3.5
Unhandled Exception:
System.ComponentModel.Win32Exception: ApplicationName='mono', CommandLine='--runtime=v4.0.30319 "/Users/smcho/Dropbox/smcho/bin/mono/NUnit.2.6.4/nunit-agent.exe" cc695a32-96df-4346-bfda-e5547d7acc87 tcp://127.0.0.1:58755/TestAgency', CurrentDirectory='', Native error= Cannot find the specified file
  at System.Diagnostics.Process.Start_noshell (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in <filename unknown>:0 
  at System.Diagnostics.Process.Start_common (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in <filename unknown>:0 
  at System.Diagnostics.Process.Start () [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:Start ()
  at NUnit.Util.TestAgency.LaunchAgentProcess (NUnit.Core.RuntimeFramework targetRuntime) [0x00000] in <filename unknown>:0 
  at NUnit.Util.TestAgency.CreateRemoteAgent (NUnit.Core.RuntimeFramework framework, Int32 waitTime) [0x00000] in <filename unknown>:0 
  at NUnit.Util.TestAgency.GetAgent (NUnit.Core.RuntimeFramework framework, Int32 waitTime) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) NUnit.Util.TestAgency:GetAgent (NUnit.Core.RuntimeFramework,int)
  at NUnit.Util.ProcessRunner.Load (NUnit.Core.TestPackage package) [0x00000] in <filename unknown>:0 
  at NUnit.ConsoleRunner.ConsoleUi.Execute (NUnit.ConsoleRunner.ConsoleOptions options) [0x00000] in <filename unknown>:0 
  at NUnit.ConsoleRunner.Runner.Main (System.String[] args) [0x00000] in <filename unknown>:0 

What might be wrong?


回答1:


I needed multiple changes for solving the issue:

Revised F# test code

I needed to use TestFixture.

module HelloWorld.Tests.Hello

open HelloWorld.Core.Hello
open NUnit.Framework
//open FsUnit

[<TestFixture>]
type TestClass() = 
    [<Test>]
    member tc.When2IsAddedTo2Expect4() = 
        Assert.AreEqual(4, 2+2)

    [<Test>]
    member tc.shouldSayHello () = 
        Assert.AreEqual("Hello", SayHello "World")

Execute mono provided unit-console

I had to execute the unit-console that is symbolically linked to /usr/bin/nunit-console@ -> /Library/Frameworks/Mono.framework/Commands/nunit-console

No need to use external dlls

I didn't have to use FsUnit.NUnit or unit.framework that I downloaded from NUNit website.

fsharpc --target:library HelloWorld.fs
fsharpc --target:library -r:HelloWorld.dll -r:nunit.framework HelloWorldTest.fs
nunit-console HelloWorldTest.dll

After the changes, everything seems to be working fine:

F# Compiler for F# 3.1 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License
F# Compiler for F# 3.1 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License
NUnit version 2.4.8
Copyright (C) 2002-2007 Charlie Poole.

Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.

Copyright (C) 2000-2002 Philip Craig.

All Rights Reserved.

Runtime Environment - 
   OS Version: Unix 14.3.0.0
  CLR Version: 2.0.50727.1433 ( 3.12.0 ((detached/a813491 Tue Dec 16 12:19:26 EST 2014) )

..
Tests run: 2, Failures: 0, Not run: 0, Time: 0.034 seconds

References

  • http://sector0.dk/?p=33
  • http://www.nunit.org/index.php?p=consoleCommandLine&r=2.2.5



回答2:


I'd like to add an updated answer to your question where your original answer helped lead me to a revised approach that is for Mono, NUnit, and F# in 2019 on macOS.

This answer applies to Mono 5.18.1.3, NUnit framework 3.12.0, and F# 4.1. There are many benefits to being able to use the latest versions including greater stability, availability of features, and integration with other contemporary tools (like IDE-based test runners).

A few differences in my answer are noted below.

  • [<TestFixture>] is deprecated now along with NUnit 2.6.4.
  • The version of nunit-console.exe bundled with Mono is also deprecated.
  • Debugging has been enabled for my build where I've included a selection of system libraries.

Additionally, the error you reported for the console runner of Native error= Cannot find the specified file is due to mono not being accessible from your PATH. This happened to me when I first attempted to use an NUnit.ConsoleRunner downloaded by NuGet where I resolved it with:

$ export PATH=$PATH:/Library/Frameworks/Mono.framework/Commands/

Source code

Following your original idea, I create a module to test in Module.fs:

namespace com.example.fs.hello.testing

module Hello
    = let SayHello = "Hello"

Then add a test in Test.fs with:

namespace com.example.fs.hello.testing

open Hello
open NUnit.Framework

type Test() =
    [<Test>]
    member x.TestCase() = Assert.AreEqual("Hello", SayHello)

Building

Finally, I add the commands to build everything to a custom location:

#/bin/sh
PROJECT_ROOT=($PWD)
DESTINATION=obj/CUSTOM
fsharpc -o:$DESTINATION/hello-world-testing.dll \
-g \
--debug:portable \
--noframework \
--define:DEBUG \
-r:$PROJECT_ROOT/packages/FSharp.Core.4.6.2/lib/net45/FSharp.Core.dll \
-r:/Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/4.5.1-api/mscorlib.dll \
-r:$PROJECT_ROOT/packages/NUnit.3.12.0/lib/net45/nunit.framework.dll \
-r:/Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/4.5.1-api/System.Core.dll \
-r:/Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/4.5.1-api/System.dll \
--target:library \
Module.fs \
Test.fs
cp $PROJECT_ROOT/packages/NUnit.3.12.0/lib/net45/nunit.framework.dll $DESTINATION

Console runner command-line testing

After a successful build, I change to the output directory. For demonstration only, I install the console runner into the current working directory and run the runner on the DLL.

$ cd obj/CUSTOM
$ nuget install NUnit.ConsoleRunner
$ mono NUnit.ConsoleRunner.3.10.0/tools/nunit3-console.exe hello-world-testing.dll

Results

If all is correct, the command-line testing ends up producing the following output.

NUnit Console Runner 3.10.0 (.NET 2.0)
Copyright (c) 2019 Charlie Poole, Rob Prouse
Tuesday, June 18, 2019 06:49:41Z

Runtime Environment
   OS Version: MacOSX 18.6.0.0 
  CLR Version: 4.0.30319.42000

Test Files
    hello-world-testing.dll


Run Settings
    DisposeRunners: True
    WorkDirectory: hello-world-fs-testing/obj/CUSTOM
    ImageRuntimeVersion: 4.0.30319
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 12

Test Run Summary
  Overall result: Passed
  Test Count: 1, Passed: 1, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
  Start time: 2019-06-18 06:49:41Z
    End time: 2019-06-18 06:49:43Z
    Duration: 1.137 seconds

Results (nunit3) saved as TestResult.xml


来源:https://stackoverflow.com/questions/29957967/system-componentmodel-win32exception-when-executing-f-nunit-unit-test-code-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!