Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing - can't move from theory to practice

It seems like every unit test example I've encountered is incredibly obvious and canned. Things like assert that x + 3 == 8, and whatnot. I just have a hard time seeing how I'd unit test real world things, like SQL queries, or if a regEx used for form validation is actually working properly.

Case in point: I'm working on two ASP.NET MVC 2 sites that are DB driven. I have a test unit solution for each of them, but have no idea what kind of tests would be useful. Most of the work the site will do is writing data to, or retrieving and organizing data from the DB. Would I simply test that the various queries successfully accessed the DB? How would I test for correctness (e.g., data being written to the proper fields, or correct data being retrieved)?

I'm just having a hard time transforming my own informal manner of testing and debugging into the more formalized, assert(x) kind of testing.

like image 859
Major Productions Avatar asked Aug 11 '10 14:08

Major Productions


People also ask

Is it hard to learn unit testing?

It's not hard to learn unit testing: The hard part is to make sure you write tests for all the possible scenarios, which will give you 100% confidence in your deployed codebase. The hard part comes when the existing code is not suitable for covering with unit testing. That kind of code is called legacy code.

Which is not true in case of unit testing?

Answer: It decrease the software development speed.


1 Answers

For unit testing to be feasible, your code will have to apply to principles of cohesion and decoupling. In fact, it will force those principles on your code as you apply it. Meaning, if your code is not well factored (i.e. OO design principles applied correctly), unit testing will be next to impossible and/or useless.

So probably, the better way for you to think about this would be 'How can I divide up all the work of my application to smaller, more cohesive pieces of code that only do one or two things and use those to assemble my application?'

Until you have internalized this mindset in terms of how you think about your code, unit testing will probably not make sense.

like image 171
Mahol25 Avatar answered Sep 29 '22 17:09

Mahol25