【问题标题】:NUnit does not find testsNUnit 找不到测试
【发布时间】:2015-12-12 13:33:29
【问题描述】:

我尝试使用 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);
        }
    }
}

什么有效?

  • Test.dll 编译成功

什么不起作用

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

NUnit 版本 2.4.8 版权所有 (C) 2002-2007 查理普尔。 版权所有 (C) 2002-2004 James W. Newkirk、Michael C. Two、Alexei A. Vorontsov。 版权所有 (C) 2000-2002 菲利普·克雷格。 版权所有。 运行环境 - 操作系统版本:Unix 15.0.0.0 CLR 版本:4.0.30319.17020(4.2.1(稳定 4.2.1.102/6dd2d0d Wed Dec 2 14:02:18 PST 2015)) 测试运行:0,失败:0,未运行:0,时间:0.055 秒

【问题讨论】:

    标签: c# msbuild mono nunit


    【解决方案1】:

    将您的测试用例属性命名为“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

    【讨论】:

    • 太好了,这就是问题所在。顺便提一句。 [Test][Test ()] 之间有区别吗,似乎 [Test] 工作得很好,所以我认为它会隐式调用 Test ()
    • [Test] 工作正常,这对我来说是一种习惯......即 [Test (ExpectedResult = "StackOverflow")],如果它丢失,我的代码格式化程序会添加它...... ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多