How do I start unit testing?

后端 未结 8 1048
盖世英雄少女心
盖世英雄少女心 2020-12-15 12:48

I know that unit testing is desirable, and I am interested in doing unit testing. The only problem is I have no idea how, or even where to start really. So my question is:

8条回答
  •  旧时难觅i
    2020-12-15 13:42

    Find-a-bug-write-a-test

    The next time you find a bug in your code base, before fixing it, write a test. The test should fail. Then, fix the bug. The test should pass.

    If the test doesn't pass, there's either a bug in your test, or a bug in your fix.

    A person will never find that bug in your code again. The unit tests will find it (and faster than a person can).

    This is definitely a small start, but it gets you into testing. Once you've got the hang of it, you'll probably start writing more tests, and eventually get a knack for how code will fail and which tests you need (for example: a test for every business rule).

    Later in your progression you setup a continuous integration server, which makes sure your codebase is always solid.

提交回复
热议问题