0

I've been dealing with a Sr Dev recently that loves unit testing. That's great! I'm not sure I agree with his testing methods though, contrived example:

function foo (String $var) {
    switch ($var) {
        case 'hello':
            return '-world';
        default:
            return 'good-bye!';
    }
}

function TestFoo() {
    $result = foo('hello');
    assertInternalType('string', $result);
}

I'm hoping the issue here is obvious, these types of tests don't actually test business logic at all, but they do increase code coverage (which he loves to show off). When asked about tests like this, I've received general "I didn't write it to test that (return values)." responses.

This is quite frustrating to me, especially as he's begun removing existing tests that do check return values in favor of this generic type-test.

This is a bad practice, correct? Just yesterday I found a bug in a function that had a passing test because of this reason - doesn't make me feel good about the code.

  • 1
    Change something, let the test pass, let QA find the failure, go doe-eyed in the meeting afterwards and say "wow, I thought our test coverage was improving? how did this happen?" – Bryan Boettcher Feb 3 '17 at 15:17
  • This is unrelated to your main problem, but you should “never throw away a test” (says Brian Kernighan) – they are far to valuable for that. If the code they are testing changes, they should be rewritten to continue testing for the same behaviour. – amon Feb 3 '17 at 15:24
  • Is this the only test? If so, then obviously there is lacking coverage on testing the functionality itself. – Matthew Feb 3 '17 at 16:25
4

An even more contrived example:

function add(int $i, int $j) {
  return $i - $j;
}

function TestAdd() {
  $result = add(2, 3);
  assertInternalType('int', $result);
}

This should immediately demonstrate the invalidity of this kind of testing. It looks like you are using PHP, so you can get the exact same amount of testing, but with the added bonus of being integrated with documentation, by simply doing this:

function add(int $i, int $j): int {
//                          ↑↑↑↑↑
  return $i - $j;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.