在单个测试中断言多个条件,还是拆分为多个测试?

编程入门 行业动态 更新时间:2024-10-08 14:40:34
本文介绍了在单个测试中断言多个条件,还是拆分为多个测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 22:44:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1400894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   断言   条件   测试   测试中

发布评论

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

>www.elefans.com

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