NUnit 找不到测试

NUnit does not find tests

我尝试使用 Mono、C# 和 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 确实包含两个简单的测试:

using 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);
        }
    }
}

什么有效?

什么不起作用

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

将您的测试用例属性命名为 "Test"(不是 "TestCase"):

更新代码:

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