Nunit没有找到测试

时间:2015-12-12 13:33:29

标签: c# msbuild mono nunit

所以我尝试使用mono,csharp和nunit设置项目。

我试图让测试运行,所以我构建了一个msbuild / xbuild Test.csproj文件:

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <OutputType>Library</OutputType>
        <OutputPath>bin</OutputPath>
        <AssemblyName>Test</AssemblyName>
        <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="nunit.framework">
          <HintPath>..\packages\NUnit\lib\net45\nunit.framework.dll</HintPath>
        </Reference>
      </ItemGroup>
      <ItemGroup>
        <Compile Include="Test.cs" />
      </ItemGroup>
      <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
      <ItemGroup>
       <ProjectReference Include="..\mainproj.csproj">
        <Name>Mainproj</Name>
       </ProjectReference>
      </ItemGroup>
     </Project>

我使用xbuild Test.csproj

构建了这个项目

文件Test.cs包含两个简单的测试:

使用NUnit.Framework;

namespace Foo.Test {

[TestFixture]
public class TestClass
{
    [TestCase]
    public void AddTest()
    {
        Assert.AreEqual(30, 15 + 15 );
    }
    [TestCase]
    public void Minus()
    {
        Assert.AreEqual(30, 15 - 15 );
    }
}
}

什么有效?

  • Test.dll编译成功

什么不起作用

nunit-console4 bin/nunit.framework.dll bin/Test.dll找不到任何测试:

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 (Stable 4.2.1.102/6dd2d0d Wed Dec  2 14:02:18 PST 2015) )


Tests run: 0, Failures: 0, Not run: 0, Time: 0.055 seconds

1 个答案:

答案 0 :(得分:4)

为您的测试用例属性命名&#34;测试&#34; (不是&#34; TestCase&#34;):

更新的代码:

using NUnit.Framework;

namespace Foo.Test {

[TestFixture ()]
public class TestClass
{
    [Test ()]
    public void AddTest()
    {
        Assert.AreEqual(30, 15 + 15 );
    }
    [Test ()]
    public void Minus()
    {
        Assert.AreEqual(30, 15 - 15 );
    }
}
}

测试的输出示例:

$>nunit-console4 Test.dll 

NUnit version 2.4.8
~~~~
..F
Tests run: 2, Failures: 1, Not run: 0, Time: 0.120 seconds

Test Case Failures:
1) Foo.Test.TestClass.Minus :   Expected: 30

注意:没有理由将nunit.framework.dll传递给nunit-console4,因为它会被扫描进行没有任何测试的测试。

参考:http://www.nunit.org/index.php?p=test&r=2.6.4