什么是测试用例?

发布于 2024-10-20 22:49:44 字数 402 浏览 6 评论 0原文

我很难找到“测试用例”这个词的严格定义。一些消息来源声称测试用例是一个扩展TestCase的类。其他来源声称测试用例是单一的测试方法。 JUnit 文档不清楚,在我看来,“测试用例”和“测试”一词的含义相同:

Test 注释告诉 JUnit,它所附加的 public void 方法可以作为测试用例运行。为了运行该方法,JUnit 首先构造该类的新实例,然后调用带注释的方法。 测试抛出的任何异常都将被 JUnit 报告为失败。如果没有抛出异常,则认为测试已成功。

那么到底什么是“测试用例”,它与“测试”有什么关系呢?

I'm having difficulties finding a hard definition of the word "test case". Some sources claim that a test case is a class that extends TestCase. Other sources claim that a test case is a single test method. The JUnit documentation is unclear, it appears to me the words "test case" and "test" mean the same:

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

So what exactly is a "test case", and what is its relationship to a "test"?

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

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

发布评论

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

评论(2

陌路终见情 2024-10-27 22:49:45

我会向您指出 TestCase 上的 JUnit 文档。其中,它描述了表示 TestCase 的三个条件:

  1. 实现 TestCase 的子类
  2. 定义存储夹具状态的实例变量
  3. 通过重写 setUp() 初始化夹具状态
  4. 通过重写 TeaDown() 进行测试后清理。

我认为设置和拆卸部分对于理解这里至关重要。测试用例不仅仅是一种带注释的方法。测试用例是该方法加上将在其上运行测试的框架的初始化和销毁​​。

因此,为了回答您的问题,测试是一种带注释的方法,它尝试验证单个方法的工作原理,而测试用例是该测试加上运行该测试的框架。

I'd point you over to the JUnit documentation on TestCase. In it, it describes three conditions for what it denotes a TestCase:

  1. implement a subclass of TestCase
  2. define instance variables that store the state of the fixture
  3. initialize the fixture state by overriding setUp()
  4. clean-up after a test by overriding tearDown().

The setUp and tearDown portion I think are critical to understanding here. It's not just simply that a test case is but one annotated method. The test case is that method plus the initialization and destruction of the frame on which a test will be run.

So to answer your question, a test is one annotated method which attempts to verify the workings of a single method while a test case is that test plus the frame in which it will be run.

雨后彩虹 2024-10-27 22:49:45

在 JUnit 上下文中,“测试用例”可以表示包含相关测试的类(在 JUnit 4 中,您不必再扩展 TestCase)或单个测试方法。该术语确实没有严格的定义。

在我的书中,测试用例是这个问题的答案:被测试的代码片段的行为是否符合预期?

测试用例可以很小:

Foo foo = new Foo();
assertEquals( "...", foo.bar(5) );

或者他们可以设置一个数据库,将其与测试数据一起使用,创建一个对象模型,然后调用几个方法来验证对象模型的修改是否按预期工作。

In the JUnit context, "test case" can mean a class which contains related tests (in JUnit 4, you don't have to extend TestCase anymore) or a single test method. There really isn't a hard definition of the term.

In my book, a test case is the answer to this question: Does the piece of code under test behave as expected?

Test cases can be tiny:

Foo foo = new Foo();
assertEquals( "...", foo.bar(5) );

or they can setup a database, will it with test data, create an object model and then invoke a couple of methods to verify that modifications of the object model work as expected.

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