NUnit 是否有从另一个单元调用测试的内置方法?

发布于 2024-08-13 21:11:24 字数 106 浏览 3 评论 0原文

我需要从不同的单元调用一个测试以在我当前的单元中使用(我所说的单元是指类)。 NUnit 是否有基础设施可以做到这一点,或者我应该继续做我正在做的事情吗?实例化类并调用方法?

谢谢!

I need to call a Test from a different unit to use in my current unit (by unit I mean class). Does NUnit have infrastructure to do that or should I just keep doing what I'm doing; instantiating the class and invoking the method?

Thanks!

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

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

发布评论

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

评论(2

Saygoodbye 2024-08-20 21:11:24

如果我理解正确的话,您在 Class1 中有一个单元测试,并且您想在 Class2 中运行单元测试。目前,在 Class1 的测试中,您正在实例化 Class2 并调用测试方法。您更喜欢 Class1 测试执行类似“Nunit.RunTest(Class2.MyTest)”的操作,

据我所知,NUnit 无法执行此操作。如果您想在多个位置使用通用代码,最简单的方法是创建两个测试都可以调用的单独方法。缺点是您无法共享两者的安装和拆卸方法。

如果您需要为这两个测试运行安装和拆卸,Steve Gilham 建议使用继承可能是更好的方法。

但一般来说,当代码更改时,您应该运行所有单元测试。每个测试都应该单独运行 - 您真的需要从另一个测试中调用一个测试吗?

If I understand you correctly, you have a unit test in Class1, and within that you want to run a unit test in Class2. Currently, within Class1's test, you are instantiating Class2 and calling the test method. You'd prefer the Class1 test do something like 'Nunit.RunTest(Class2.MyTest)'

As far as I know, there is no way to do this from NUnit. If you have common code you want to use in multiple locations the easiest way is to create a separate method that both tests can call. The downside is that you wouldn't be able to share Setup and Teardown methods for the two.

If you need to run Setup and Teardown for both tests, Steve Gilham's suggestion to use inheritance might be a better approach.

In general though, you should be running all unit tests when code changes. Each test should be run on it's own - do you really need to call one test from within another?

请恋爱 2024-08-20 21:11:24

您能详细说明一下这里的用例是什么吗?

最明显的自动方法是继承。如果您当前的测试类是另一个测试类的子类,那么它的所有测试都将被自动拉入。这对于测试对象层次结构非常有用,其中基类的单元测试类本身就是每个派生类的测试类的基类。

Could you elaborate as to what is the use-case here?

The most obvious automatic method is inheritance. If your current test class subclasses the other test class, then all its tests will be pulled in automatically. This is useful for testing object hierarchies, where the unit test class for the base class is itself the base class for the test classes for each derived class.

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