System.ComponentModel.Win32Exception 在使用 Mono 执行 F# Nunit 单元测试代码时
System.ComponentModel.Win32Exception when executing F# Nunit unit test code with Mono
我正在尝试在 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")
我编译代码和单元测试:
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
可能出了什么问题?
我需要进行多项更改才能解决问题:
修改后的 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")
执行单声道提供的单元控制台
我必须执行符号链接到 /usr/bin/nunit-console@ -> /Library/Frameworks/Mono.framework/Commands/nunit-console
的单元控制台
无需使用外部 dll
我不必使用从 NUNit 网站下载的 FsUnit.NUnit
或 unit.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
参考资料
我想为您的问题添加一个更新的答案,您的原始答案帮助我找到了 修改后的方法 ,该方法适用于 Mono、NUnit、和 2019 年在 macOS 上的 F#。
此答案适用于 Mono 5.18.1.3、NUnit 框架 3.12.0 和 F# 4.1。能够使用最新版本有很多好处,包括更高的稳定性、功能的可用性以及与其他现代工具的集成(例如 IDE-based test 运行ners)。
下面列出了我的回答中的一些差异。
[<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
控制台 运行ner command-line 测试
构建成功后,我切换到输出目录。仅用于演示,我将控制台 运行ner 安装到当前工作目录,并在 DLL 上安装 运行ner 运行ner。
$ cd obj/CUSTOM
$ nuget install NUnit.ConsoleRunner
$ mono NUnit.ConsoleRunner.3.10.0/tools/nunit3-console.exe hello-world-testing.dll
结果
如果一切正确,command-line 测试最终会产生以下输出。
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
我正在尝试在 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")
我编译代码和单元测试:
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
可能出了什么问题?
我需要进行多项更改才能解决问题:
修改后的 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")
执行单声道提供的单元控制台
我必须执行符号链接到 /usr/bin/nunit-console@ -> /Library/Frameworks/Mono.framework/Commands/nunit-console
无需使用外部 dll
我不必使用从 NUNit 网站下载的 FsUnit.NUnit
或 unit.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
参考资料
我想为您的问题添加一个更新的答案,您的原始答案帮助我找到了 修改后的方法 ,该方法适用于 Mono、NUnit、和 2019 年在 macOS 上的 F#。
此答案适用于 Mono 5.18.1.3、NUnit 框架 3.12.0 和 F# 4.1。能够使用最新版本有很多好处,包括更高的稳定性、功能的可用性以及与其他现代工具的集成(例如 IDE-based test 运行ners)。
下面列出了我的回答中的一些差异。
[<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
控制台 运行ner command-line 测试
构建成功后,我切换到输出目录。仅用于演示,我将控制台 运行ner 安装到当前工作目录,并在 DLL 上安装 运行ner 运行ner。
$ cd obj/CUSTOM
$ nuget install NUnit.ConsoleRunner
$ mono NUnit.ConsoleRunner.3.10.0/tools/nunit3-console.exe hello-world-testing.dll
结果
如果一切正确,command-line 测试最终会产生以下输出。
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