使用Mono执行F#Nunit单元测试代码时的System.ComponentModel.Win32Exception

时间:2015-04-30 02:23:44

标签: unit-testing f# nunit

我正在尝试在Mono上使用F#unittest。我使用Mac OS X.我下载并复制了NUNit框架。

我有以下环境

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

这是F#代码:

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

这是它的单位测试。

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

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

我编译代码和unittest:

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

我运行测试:mono $console HelloWorldTest.dll以获取错误消息。

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 

可能出现什么问题?

2 个答案:

答案 0 :(得分:1)

我想为您的问题添加更新的答案,您的原始答案帮助我找到了修订的方法,该方法适用于macOS上的 Mono,NUnit和F#。

此答案适用于Mono 5.18.1.3,NUnit框架3.12.0和F#4.1。能够使用最新版本有很多好处,包括更高的稳定性,功能的可用性以及与其他现代工具(例如基于IDE的测试运行器)的集成。

以下是我回答中的一些差异。

  • [<TestFixture>]和NUnit 2.6.4现在已被弃用。
  • 与Mono捆绑在一起的nunit-console.exe版本也已弃用。
  • 已为我的构建启用了调试功能,其中包含了一些系统库。

此外,您为Native error= Cannot find the specified file的控制台运行程序报告的错误是由于mono无法访问PATH。当我第一次尝试使用NuGet下载的NUnit.ConsoleRunner时,发生了这种情况,我使用以下方法解决了该问题:

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

源代码

按照您的原始想法,我创建一个模块在Module.fs中进行测试:

namespace com.example.fs.hello.testing

module Hello
    = let SayHello = "Hello"

然后在Test.fs中添加以下测试:

namespace com.example.fs.hello.testing

open Hello
open NUnit.Framework

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

建筑物

最后,我添加命令以将所有内容构建到自定义位置:

#/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

控制台运行程序命令行测试

成功构建后,我转到输出目录。仅出于演示目的,我将控制台运行程序安装到当前工作目录中,然后在DLL上运行运行程序。

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

结果

如果一切正确,命令行测试最终将产生以下输出。

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

答案 1 :(得分:0)

我需要多项更改才能解决问题:

修订了F#测试代码

我需要使用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")

执行mono提供的unit-console

我必须执行与/usr/bin/nunit-console@ -> /Library/Frameworks/Mono.framework/Commands/nunit-console

符号链接的单位控制台

无需使用外部dll

我没有必要使用我从NUNit网站下载的FsUnit.NUnitunit.framework

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

改变之后,一切似乎都运转良好:

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

参考