How do I start unit testing?

2019-01-23 09:01发布

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: How do I learn about and start unit testing? Specifically I frequently write Java code in NetBeans and C# code in Visual Studio and am interested in what tools to use and how to get my feet wet. Can anyone offer any advice for an absolute unit testing n00b?

I know that there are a lot of somewhat similar questions out there but I am less interested in why and more interested in how.

8条回答
We Are One
2楼-- · 2019-01-23 09:23

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-23 09:27

Check out The Art of Unit Testing by Roy Osherove, it's a good book for beginners since it starts at the very beginning.

查看更多
登录 后发表回答