什么是测试用例?
我很难找到“测试用例”这个词的严格定义。一些消息来源声称测试用例是一个扩展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 thepublic 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会向您指出 TestCase 上的 JUnit 文档。其中,它描述了表示 TestCase 的三个条件:
我认为设置和拆卸部分对于理解这里至关重要。测试用例不仅仅是一种带注释的方法。测试用例是该方法加上将在其上运行测试的框架的初始化和销毁。
因此,为了回答您的问题,测试是一种带注释的方法,它尝试验证单个方法的工作原理,而测试用例是该测试加上运行该测试的框架。
I'd point you over to the JUnit documentation on TestCase. In it, it describes three conditions for what it denotes a TestCase:
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.
在 JUnit 上下文中,“测试用例”可以表示包含相关测试的类(在 JUnit 4 中,您不必再扩展
TestCase
)或单个测试方法。该术语确实没有严格的定义。在我的书中,测试用例是这个问题的答案:被测试的代码片段的行为是否符合预期?
测试用例可以很小:
或者他们可以设置一个数据库,将其与测试数据一起使用,创建一个对象模型,然后调用几个方法来验证对象模型的修改是否按预期工作。
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:
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.