Sell ​​me on unit testing

I have been a .Net developer for a long time and try to wrap my head around real, practical unit testing.

In particular, I am considering unit testing of ASP.Net MVC 3 projects.

I read a decent amount about it and I think that I understand it at the academic level (i.e. basically guarantees that the changes you make will not destroy other things). However, all the tests I've seen in the examples are trivially stupid things that would be pretty obvious anyway. (Does this controller return a view with this name?).

So, maybe I missed something or just didn’t see really good test examples or anything else, but it really looks like a shitty lot of extra work and complexity with ridicule, ioc, etc., and I'm just not seeing counter balance.

Teach me please :)

+4
source share
2 answers

With proper unit testing, it's almost trivial to catch corner cabinets that would otherwise slip past you. Let's say you have a method that returns a list of items, and the list of items is retrieved from some table. What happens if this table is not filled out correctly (for example, if one of the columns has an empty value) or if they change the type of the column so that your ORM tool does not display as you thought? Unit tests will help to catch these cases before you start production, and someone will delete all the data in your table.

0
source

basically guarantees that the changes you make will not destroy other things.

This is not unit testing, but regression testing . Unit tests are used to test the smallest pieces of code separately (usually at the class level). This is not very useful in situations where the projects are small or there are no classes.

There are many examples where some form of unit testing (mixed with other forms, as a rule, I like to use mock testing if I have time, for example) is very useful. Let's say you have a large project with about 20 classes, and you get an error in one of them. You may need to carefully examine each class and make sure their methods return the correct information. This is unit testing.

In short, unit testing is used when you need to test a specific class or specific methods to make sure they are functional. It is much easier to find a problem with the program when you are working with the smallest units, rather than strolling through the whole program to find out which method does not work correctly.

0
source

All Articles