开发者

How to start with unit testing in C#?

开发者 https://www.devze.com 2023-03-03 21:31 出处:网络
I have a simple task to display a fruit object based on certain conditions. For example, if I need to show a banana I need to make sure the other fruits don\'t display.

I have a simple task to display a fruit object based on certain conditions. For example, if I need to show a banana I need to make sure the other fruits don't display.

For simplicity's sake, let's say they are 4 possible fruits. Do I need to test all the possible combina开发者_StackOverflow中文版tions?

Test1 if banana not strawberry

Test2 if banana not apple

Etc.


You need to test as many combinations as there are code paths, for sure. If there's no strawberry-specific or apple-specific code, then you don't need to test both banana v. strawberry and banana v. apple. But if there's different code for a given fruit, you need to test it, as well as one of the others; and if there's different code based on the number of fruits, then you need to test 0, 1, 2, N-1, N, and N+1, whatever the largest value of N is. In general, you need to test enough to convince yourself additional tests wouldn't help.


Depends on few things. But one way would be just to make sure you only have banana when you expect a banana. I'm sure there are differences between types, so you can assert on that (eg. assert names of all objects are "banana" only).


This depends on things such as if your code is even capable of displaying more than one fruit at once (by accident or otherwise). The extremely safe choice would be "Yes, test everything!"

0

精彩评论

暂无评论...
验证码 换一张
取 消