{"id":48,"date":"2015-02-22T19:00:30","date_gmt":"2015-02-22T19:00:30","guid":{"rendered":"http:\/\/dracony.org\/?p=48"},"modified":"2015-02-22T19:07:24","modified_gmt":"2015-02-22T19:07:24","slug":"unit-tests-are-not-enough","status":"publish","type":"post","link":"https:\/\/dracony.org\/unit-tests-are-not-enough\/","title":{"rendered":"Unit Tests are not enough"},"content":{"rendered":"
For the last half a year I have been refactoring the next version of PHPixie ORM and writing unit tests for it. My goal is to bring it to 100% coverage ( right now it’s at 97% ). But as others have already stated, 100% coverage doesn’t really mean there are no bugs in the code, all it means is that the components are behaving in the way you intended them to.<\/p>\n
One huge problem with unit testing is that it may not detect wrong parameters in method calls. For example take a look at this method:<\/p>\n
\r\n\/\/Checks whether string $a contains string $b\r\npublic function contains($a, $b) {\r\n ....\r\n}\r\n<\/pre>\nLet’s say we have it successfully unit tested and continue to a different method that relies on contains()<\/em>:<\/p>\n
\r\n\/\/Checks whether string $string contains 'cat'\r\npublic function containsCat($string) {\r\n return $this->stringTools->contains($string, 'cat');\r\n}\r\n<\/pre>\nNow we unit test the containsCat()<\/em> by mocking the contains()<\/em> call. Our unit tests pass and all is great.<\/p>\n
A week after that someone decides to modify contains($a, $b)<\/em> by reordering the arguments. So instead of checking whether $a contains $b it will now check whether $b contains $a. He then fixes the tests for that method and it seems everything is ok. Except that now our containsCat()<\/em> method is broken, since it passes arguments in the wrong order. Out unit test will not tell us that because the call to contains()<\/em> has been mocked.<\/p>\n
This issue is somewhat mitigated by using type hinting, at least then if you reorder parameters of different types you may get an error stating that. This is why I really want PHP 7 to get static type hints, but even then, as with the contains()<\/em> example, you still are not safe.<\/p>\n
That is why you also need integration and functional tests where you can check the whole system or a set of components working together. These tests are usually much easier to write then unit tests, since they require using actual dependencies and only minimal mocking. They also help you save more time, as unlike unit tests they rarely need to change after code refactoring. <\/p>\n
Actually I came to a conclusion that you should start with having functional tests first and only then drilling down to writing unit tests. And perhaps if you manage to cover over 80% of your codebase with functional tests you may find it fitting to skip unit tests altogether in some cases. This is especially true for websites where having behavioral tests ( like Behat ) not only provides you with means of testing the actual pages rendered, but also acts as a spec for the entire system.<\/p>\n","protected":false},"excerpt":{"rendered":"
For the last half a year I have been refactoring the next version of PHPixie ORM and writing unit tests for it. My goal is to bring it to 100% coverage ( right now it’s at 97% ). But as others have already stated, 100% coverage doesn’t really mean there are no bugs in the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":3,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts\/48\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}