Nunit-console - .csproj文件中的预期是什么?

时间:2015-11-20 10:50:28

标签: c# msbuild mono nunit csproj

我目前正在使用C#进行一些测试(使用Mono和NUnit,所有这些都在OS X上,但它应该在Travis上运行)。我没有安装Visual Studio并设法获得一个“工作”的.csproj文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Compile">
  <Target Name="Compile">
    <Csc Sources="@(Compile)" References="@(Reference)" TargetType="Library" />
  </Target>
  <Target Name="Test" DependsOnTargets="Compile">
    <Exec Command="nunit-console nunit.csproj" />
  </Target>
  <ItemGroup>
    <Reference Include="nunit.framework.dll" />
  </ItemGroup>
  <ItemGroup>
     <Compile Include="../csharp/class/CoffeeMachine.cs" />
     <Compile Include="tests/Actionwords.cs" />
     <Compile Include="tests/ProjectTest.cs" />
  </ItemGroup>
</Project>

编译工作正常,但我无法执行测试,我收到以下错误:

╰─$ nunit-console nunit.csproj
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: 4.0.30319.17020 ( 4.2.0 (Stable 4.2.0.179/a224653 Tue Oct  6 11:27:49 PDT 2015) )

Unhandled Exception:
System.ArgumentException: Invalid project file format: nunit.csproj ---> System.NullReferenceException: Object reference not set to an instance of an object
   at NUnit.Util.VSProject.LoadMSBuildProject (System.String projectDirectory, System.Xml.XmlDocument doc) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/VSProject.cs:line 257
   at NUnit.Util.VSProject.LoadProject (System.String projectDirectory, System.Xml.XmlDocument doc) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/VSProject.cs:line 210
   at NUnit.Util.VSProject.Load () in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/VSProject.cs:line 184
  --- End of inner exception stack trace ---
   at NUnit.Util.VSProject.ThrowInvalidFormat (System.String projectPath, System.Exception e) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/VSProject.cs:line 307
   at NUnit.Util.VSProject.Load () in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/VSProject.cs:line 197
   at NUnit.Util.VSProject..ctor (System.String projectPath) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/VSProject.cs:line 56
   at NUnit.Util.NUnitProject.FromVSProject (System.String vsProjectPath) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/NUnitProject.cs:line 234
   at NUnit.Util.NUnitProject.LoadProject (System.String path) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ClientUtilities/util/NUnitProject.cs:line 161
   at NUnit.ConsoleRunner.ConsoleUi.MakeRunnerFromCommandLine (NUnit.ConsoleRunner.ConsoleOptions options) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ConsoleRunner/nunit-console/ConsoleUi.cs:line 214
   at NUnit.ConsoleRunner.ConsoleUi.Execute (NUnit.ConsoleRunner.ConsoleOptions options) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ConsoleRunner/nunit-console/ConsoleUi.cs:line 58
   at NUnit.ConsoleRunner.Runner.Main (System.String[] args) in /private/tmp/mono20151006-19632-tqpley/mono-4.2.0/mcs/nunit24/ConsoleRunner/nunit-console/Runner.cs:line 63

注意:如果我执行nunit-console hiptest.publisher.samples.dll,它就像魅力一样。

我的猜测是NUnit希望.csproj文件告诉测试目标是什么。我试图在PropertyGroup下添加一个AssemblyName组,但它没有任何区别。

最大的问题是我找不到任何关于Nunit对.csproj内容的期望的文档(我主要是通过VS或使用MSBuild社区任务找到所有内容的例子,这听起来很不错但很明显不适合我的环境(Mono,OS X和一个不同的NUnit包而不是Mono发布的那个(不是在那个项目上,另一个使用Specflow但是如果我可以修复它,那么应该修复第二个)一个))。

提前致谢, 文森特

1 个答案:

答案 0 :(得分:0)

当将NUnit控制台2.4.x和3.x与ExplicitWidth = <nnn> ExplicitHeight = <nnn> 文件一起使用时,它需要一个有效且相当完整的MSBuild / xbuild项目来确定它将在其中测试的程序集名称,目录位置和命名空间。基于库的项目(如果已定义正确的入口点,则可以使用基于Exe的项目)。在这个例子中,我使用了NUnit 2.4.x和3.x console ,因为存在执行差异,但正在使用相同的.csproj,因此如果您使用的是MonoDevelop / Xamarin Studio,则构建IDE在NUnit 2.4.x测试垫中也可以工作。此外,我假设您还将使用Mono的.csproj在Travis上构建您的项目,因此需要完整的xbuild

NUnit 3.0控制台(通过nuget安装):

.csproj

NUnit 2.4.8(通过Mono安装):

注意:在解析mono $(MTOOLS)/nunit3-console.exe nunit-lib/nunit-lib.csproj --config=Debug NUnit Console Runner 3.0.5797 Copyright (C) 2015 Charlie Poole Runtime Environment OS Version: MacOSX 15.0.0.0 CLR Version: 4.0.30319.17020 Test Files nunit-lib/nunit-lib.csproj Errors and Failures 1) Failed : nunitlib.Test.TestCase Expected string length 8 but was 5. Strings differ at index 0. Expected: "Overflow" But was: "Stack" -----------^ at nunitlib.Test.TestCase () in <filename unknown>:line 0 Test Run Summary Overall result: Failed Tests run: 1, Passed: 0, Errors: 0, Failures: 1, Inconclusive: 0 Not run: 0, Invalid: 0, Ignored: 0, Explicit: 0, Skipped: 0 Start time: 2015-11-20 12:36:28Z End time: 2015-11-20 12:36:28Z Duration: 0.132 seconds 文件并创建预期的CIL /程序集位置时,NUnit控制台2.4.x是broken,因为它是一个硬编码的Windows样式的目录分隔符,使用.csproj来解决它。这不是3.0中的问题。

NUnit Console 2.4.x没有MONO_IOMAP:

MONO_IOMAP

NUnit Console 2.4.x with MONO_IOMAP:

nunit-console nunit-lib/nunit-lib.csproj -config=Debug
~~~~
Unhandled Exception:
System.IO.DirectoryNotFoundException: Directory "/Users/sushi/code/XamTests/nunit-lib/nunit-lib/bin\Debug" not found.
~~~~

.csproj示例中使用的示例:

MONO_IOMAP=all nunit-console nunit-lib/nunit-lib.csproj -config=Debug

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 15.0.0.0
  CLR Version: 4.0.30319.17020 ( 4.2.1 (explicit/8862921 Thu Oct 29 17:09:16 EDT 2015) )

.F
Tests run: 1, Failures: 1, Not run: 0, Time: 0.115 seconds

Test Case Failures:
1) nunitlib.Test.TestCase :   Expected string length 8 but was 5. Strings differ at index 0.
  Expected: "Overflow"
  But was:  "Stack"
  -----------^