What are unit tests and why should I care?

后端 未结 8 1453
礼貌的吻别
礼貌的吻别 2020-12-13 05:13

Okay, I develop web applications in PHP and JavaScript and a lot of times here on Stack Overflow I have seen the word unit test passing by, but nowhere on the websi

8条回答
  •  天命终不由人
    2020-12-13 05:33

    Unit tests are for any code you wish to maintain.

    In a nutshell, the idea is to write many small tests, each of which can be run in isolation, and test the smallest possible part of your codebase (often individual classes or individual functions). If I give this function the input it expects, does it return the output I expect? If it does, that means the rest of the application can pretty much assume it works. And if it doesn't, I'd rather catch the error in a small, simple, isolated unit test function than trying to trace it through the entirety of my application.

    Of course, it also requires you to be fairly disciplined in how you write your code, both because it has to be possible to isolate individual functions or classes to test them, and because, well, the tests don't write themselves. You have to do that. ;)

    Given the quality of most of the PHP code I've seen, I'd say unit testing definitely has its place in the PHP community. More so than in almost any other language, in fact. ;)

提交回复
热议问题