Mono 下的 NUnit 测试

发布于 2024-09-04 11:13:19 字数 1292 浏览 3 评论 0原文

我有一个 mut.cs 如下。

using System;

namespace ns
{
    public class Arith {
        public int Add(int x, int y) {
            return x + y;
        }
        public int Mul(int x, int y) {
            return x * y;
        }
    }
}

我为此想出了一个单元测试 - mut_test.cs

using NUnit.Framework;
using System;
using ns;

namespace Unit.Tests {
    [TestFixture]
    public class ArithTests {
        private Arith m_arith = null;
        [Setup]
        public void Setup()
        {
            m_arith = new Arith();
        }
        [Test]
        public void ValidateAdd()
        {
            int res = m_arith.Add(10,10);
            Assert.AreEqual(20, res);
        }
        [TearDown]
        public void TearDown()
        {
            m_arith = null;
        }
    }
}

我运行了以下命令。

gmcs -debug -t:library -r:System -r:$NUNITLIB -out:mut.dll mut_test.cs mut.cs 

但我收到以下错误。 $NUNITLIB 别名为 $NUNITLIB=$NUNITBIN/framework/nunit.framework.dll

mut_test.cs(9,10): error CS0118: `Unit.Tests.ArithTests.Setup()' is a `method' but a `type' was expected
mut_test.cs(9,10): error CS0246: The type or namespace name `SetupAttribute' could not be found. Are you missing a using directive or an assembly reference?

可能出了什么问题?

I have a mut.cs as follows.

using System;

namespace ns
{
    public class Arith {
        public int Add(int x, int y) {
            return x + y;
        }
        public int Mul(int x, int y) {
            return x * y;
        }
    }
}

I came up with a Unit test for this - mut_test.cs

using NUnit.Framework;
using System;
using ns;

namespace Unit.Tests {
    [TestFixture]
    public class ArithTests {
        private Arith m_arith = null;
        [Setup]
        public void Setup()
        {
            m_arith = new Arith();
        }
        [Test]
        public void ValidateAdd()
        {
            int res = m_arith.Add(10,10);
            Assert.AreEqual(20, res);
        }
        [TearDown]
        public void TearDown()
        {
            m_arith = null;
        }
    }
}

I ran the following command.

gmcs -debug -t:library -r:System -r:$NUNITLIB -out:mut.dll mut_test.cs mut.cs 

But I get the following error. $NUNITLIB is aliased as $NUNITLIB=$NUNITBIN/framework/nunit.framework.dll

mut_test.cs(9,10): error CS0118: `Unit.Tests.ArithTests.Setup()' is a `method' but a `type' was expected
mut_test.cs(9,10): error CS0246: The type or namespace name `SetupAttribute' could not be found. Are you missing a using directive or an assembly reference?

What might be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半夏半凉 2024-09-11 11:13:19

您要查找的属性称为“SetUp”,而不是“Setup”。

请参阅此处:NUnit 文档 - 设置

The attribute you're looking for is called SetUp, not Setup.

See here: NUnit documentation - Setup

千年*琉璃梦 2024-09-11 11:13:19

NUnit 中的 SetUp 的大写字母为 U

我讨厌它的拼写方式(尽管它似乎是 由于是动词而拼写正确),但这就是问题的根源。

SetUp in NUnit has a capital U.

I hate that it's spelled that way (even though it seems to be correctly spelled due to being a verb), but that's the source of your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文