在单个测试中断言多个条件,还是拆分为多个测试? [英] Assert multiple conditions in a single test, or split into multiple tests?

查看:17
本文介绍了在单个测试中断言多个条件,还是拆分为多个测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您正在测试如下所示的计数函数,在一个函数中测试该函数的多项内容与为每个测试都设置一个测试函数是正确"还是错误"?

If you were testing a count function like the one below, is it considered to be 'right' or 'wrong' to test multiple things for the function in one function vs having a test function for each of the tests?

function testGetKeywordCount() {
    $tester = $this -> getDatabaseTester($this -> CompleteDataFile);
    $tester -> onSetUp();

    $KeywordID = 0;
    $this -> setExpectedException('InvalidArgumentException');
    $this -> keyword -> getKeywordCount($KeywordID,'Active');

    $KeywordID = 1;
    $this -> setExpectedException('InvalidArgumentException');
    $this -> keyword -> getKeywordCount($KeywordID,'InvalidStatus');

    $this -> assertEquals(1, $this -> keyword -> getKeywordCount($KeywordID,'Active'));

    $tester -> onTearDown();
}

推荐答案

对于每个场景都有一个测试用例是理想的.但是,在某些情况下,在一个测试用例中测试多个场景会更方便(从实现工作的角度来看更有效).如果您使用的框架不会在第一次失败时停止,而是尝试在测试用例中尽可能多地执行,则该框架适用于每个测试用例的多个场景.

Having one test case for each scenario is ideal. However, in some cases it is more convenient (efficient from implementation effort point of view) to test more than one scenario in one test case. If you use a framework that doesn't stop on first failure, but tries to execute as much as possible in a test case, that framework is appropriate for multiple scenarios per test case.

我更喜欢在单元测试上花费尽可能少的时间,并且在这段时间内仍然获得尽可能多的覆盖率.

I prefer to spend as less time as possible on unit testing, and still get as most coverage as possible in that time.

最后,如何实施单元测试并不重要,而更重要的是这些测试的正确性.

In the end, it matters less how you implement the unit test, but more the correctness of those tests.

这篇关于在单个测试中断言多个条件,还是拆分为多个测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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