使用jUnit进行数据驱动的测试 [英] Data-driven tests with jUnit

查看:218
本文介绍了使用jUnit进行数据驱动的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jUnit中使用什么来编写数据驱动的测试?

What do you use for writing data-driven tests in jUnit?

(我对数据驱动的测试的定义)是一种测试,它从某些外部源(文件,数据库等)读取数据,每行/文件/任何内容执行一个测试,并在测试中显示结果跑步者就像您有单独的测试一样-每次运行的结果将单独显示,而不是一个庞大的汇总.

(My definition of) a data-driven test is a test that reads data from some external source (file, database, ...), executes one test per line/file/whatever, and displays the results in a test runner as if you had separate tests - the result of each run is displayed separately, not in one huge aggregate.

推荐答案

在JUnit4中,您可以使用参数化测试运行程序来进行数据驱动的测试.

In JUnit4 you can use the Parameterized testrunner to do data driven tests.

它的文档记录不是很好,但是基本思想是创建一个静态方法(用@Parameters注释),该方法返回Object数组的Collection.这些数组中的每一个都用作测试类构造函数的参数,然后可以使用构造函数中设置的字段来运行常规的测试方法.

It's not terribly well documented, but the basic idea is to create a static method (annotated with @Parameters) that returns a Collection of Object arrays. Each of these arrays are used as the arguments for the test class constructor, and then the usual test methods can be run using fields set in the constructor.

您可以使用@Parameters方法编写代码以读取和解析外部文本文件(或从其他外部源获取数据),然后可以通过编辑该文件来添加新测试,而无需重新编译测试

You can write code to read and parse an external text file in the @Parameters method (or get data from another external source), and then you'd be able to add new tests by editing this file without recompiling the tests.

这篇关于使用jUnit进行数据驱动的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆