T
T
TAnonim2018-07-27 14:20:57
PHP
TAnonim, 2018-07-27 14:20:57

Correct approach to testing in php applications?

The documentation for PHPUnit and some articles contain code like this:

public function testMethod() {
        $expectedResult = 100;
        $result = $this->testclass->testMethod(1, 2);
        $this->assertEquals($expectedResult, $result);
}

Surely this is better than writing var_dumps and manually checking the output values ​​of methods. But the question is: this test lets me know if the method works correctly on the values ​​1 and 2. And if I write 1000 more tests of this type, it will only guarantee that everything is fine on these 1000 data sets, and suddenly the error is just in another, 1001st set? Or I somehow do not understand the concept of unit tests.
And yet, on GitHub you can often find the tags "99% test coverage", "X% test coverage". How do they know that a product is n percent tested? How can I know that my method is 100% tested?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Mokevnin, 2018-07-27
@TAnonim

coverage tells about the coverage of specific rows by tests. If during the test run some lines of the source code were not affected, then coverage will be less than 100%. On average, it is considered that a good coverage is > 80%. 100% is too difficult and expensive to achieve (and not necessary).
> And if I write another 1000 of this type of tests, this will only guarantee that everything is fine on these 1000 data sets, and what if the error is just in another, 1001st set?
No one will give you guarantees. For guarantees, there is a formal verification, but this is more about mathematics than about programming. And so it is the responsibility of the programmer to test edge cases. It's okay that you missed them usually does not happen. Just if a bug occurs, then first add a test that reproduces it, and then fix it.
From the interesting:
* property-based testing https://en.wikipedia.org/wiki/QuickCheck
* bdd behat.org/en/latest/guides.html
* browser tests https://codeception.com/
* how to write tests ( concept story) https://ru.hexlet.io/blog/posts/how-to-test-code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question