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

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

问题描述

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

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

(我的定义)数据驱动测试是一种从某些外部源(文件、数据库等)读取数据的测试,每行/文件/任何内容执行一个测试,并在测试中显示结果runner 就像您进行了单独的测试一样 - 每次运行的结果都单独显示,而不是一个巨大的聚合.

(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 注释).这些数组中的每一个都用作测试类构造函数的参数,然后可以使用构造函数中设置的字段运行通常的测试方法.

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天全站免登陆