更改每个PHPUnit测试的数据集(Changing datasets for each PHPUnit Test)

编程入门 行业动态 更新时间:2024-10-27 22:26:06
更改每个PHPUnit测试的数据集(Changing datasets for each PHPUnit Test)

我在PHPUnit测试中有点不自觉,我需要涵盖一个方法的多个场景。 该方法获取表的行并根据它做出决策,因此如果我有0行匹配我的查询,它将执行一些操作,否则,它将执行另一个。

我做了DBUnit的设置,一切都在运行,但整个类运行相同的getDataSet方法,因此它运行相同的MySQLXMLDump。

例如:

testScenarioA - >空表。

testScenarioB - >表有数据。

我需要在我的类上的每个测试函数加载一个foo XML。 我怎么能做到这一点?

i'm kinda noobie in PHPUnit Testing and I need to cover multiple scenarios of a method. The method takes the rows of a table and make decisions based on it, so if I have 0 rows matching my query, it will execute some action, else, it will execute another.

I did the setup of DBUnit and everything is running, but the whole class is running the same getDataSet method so its running the same MySQLXMLDump.

For example:

testScenarioA -> Empty table.

testScenarioB -> Table has data.

I need to each test function on my class load a foo XML. How can I accomplish that?

最满意答案

因此,如果您在Unit Test类中有数据集,则可以让数据集返回空结果和一些数据

public static function dataForTest() { return [ 'empty' => [getEmptyDataset()] 'results' => [getResults()] ]; }

然后在用于测试的函数中将其与数据集一起使用

/** * @dataProvider dataForTest */ public function testSyncUser($dataTypes) { foreach ($dataTypes as $dataType) { // Run tests } }

或者,您可以创建一个辅助类,与单元测试一起使用,以便即时获取测试数据。 PHPUnit在实例化Test Unit类之前生成所有数据集,因此有时这可能是一种有用的方法。

So if you have a dataset in your Unit Test class, you could have the dataset return empty results and some data

public static function dataForTest() { return [ 'empty' => [getEmptyDataset()] 'results' => [getResults()] ]; }

Then in the function used for the tests use that with your dataset

/** * @dataProvider dataForTest */ public function testSyncUser($dataTypes) { foreach ($dataTypes as $dataType) { // Run tests } }

Alternatively you could create a helper class to use alongside your unit tests to get data for the tests on the fly. PHPUnit generates all the data sets prior to instantiation of the Test Unit class so sometimes this can be a useful approach.

更多推荐

本文发布于:2023-07-31 06:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   数据   PHPUnit   Test   datasets

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!