以下情况:
public class InputFilter(){
public function isInputValid($input) : bool
{
$isValid = false;
if($inputA && $inputB && $inputC){
return true;
}
if($inputD && $inputE){
return true;
}
...
return $isValid;
}
}
public Test(){
//1. TestCase
isInputValid_ValidInputABCGiven_ShouldReturnTrue(){
$input[a => true, $b => true, c => true, d => false, e => false]
assertSame(true, InputFilter->isInputValid, '')
}
//2. TestCase
isInputValid_ValidInputBCGiven_ShouldReturnTrue{
$input[a => false, $b => false, c => false, d => true, e => true]
assertSame(true, InputFilter->isInputValid, '')
}
}
我真的不知道如何测试这种情况。 让我们想象第一个测试用例失败,然后我不知道这是否是由于以下事实 第一个if语句(a && b && c)失败,第二个if语句(d && e)有问题。 如果第二次测试也失败了,我将不知道这些语句中的哪一个导致了失败。真正的问题是,如果条件...,则有多个...
有什么方法可以测试该方法,以便我知道测试为什么失败了吗?
答案 0 :(得分:1)
如果语句从不“有问题”。他们完全按照他们所说的做,100%的时间。无需测试if语句。
但是,您应该测试各个方案。
例如:
function testResultReturnsFalseForADTrue
{
$input[a => true, $b => false, c => false, d => true, e => false]
assertFalse(InputFilter->isInputValid);
}
如果第二项测试也失败了,我将不知道其中哪一项 语句导致失败
哪个语句导致失败并不重要。如果您正在使用A&B测试该输入返回false,然后返回true,则您已经有足够的信息。
当您期望错误或反之亦然时,无法指出返回true的确切行。
答案 1 :(得分:1)
The goal of unit-testing (and testing in general) is to find bugs. The approach is to exercise some code in a controlled way such that you have a clear expectation about how it behaves. If during test execution the actual behaviour differs from the expected behaviour, you have to start your investigation where the problem is. You may have found a bug - or possibly the test case is wrong ! This shows you can not even assume that the problem is actually in the code. Often, in case of a failing test, debugging starts.
However, if you have code as you have shown, there should certainly be many more test cases. The pattern of failing and succeeding tests can help you in your investigation. It is a quality criterion of a test suite that you can easily track down a potential problem by looking at the list of test results.
答案 2 :(得分:0)
制作:
if($inputA && $inputB && $inputC){
return true;
}
您在函数中调用的函数。然后:
if($inputD && $inputE){
return true;
}
您调用的另一个函数。
然后对这些功能进行测试