More unit testing questions....
-
Hey everyone, I have a few small questions on unit testing. I am kind of doing this backwards because my team is starting to make everyone do unit tests on there code and they want me to start with code that I have already written. So I am looking through my code and not sure what functions to unit test. I know you dont usually unit test GUI, or set and get functions. I have functions that load tables, generate reports and emails. Should I also unit test these functions? Thanks for all of your advice! :-D
There are 10 kinds of people in this world. Those who understand binary and those who don't. We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.
-
Hey everyone, I have a few small questions on unit testing. I am kind of doing this backwards because my team is starting to make everyone do unit tests on there code and they want me to start with code that I have already written. So I am looking through my code and not sure what functions to unit test. I know you dont usually unit test GUI, or set and get functions. I have functions that load tables, generate reports and emails. Should I also unit test these functions? Thanks for all of your advice! :-D
There are 10 kinds of people in this world. Those who understand binary and those who don't. We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.
Well, I try to unit test anything that is not trivial and has a well-defined result such that a test can be automated. For functions that load tables, you might be able to unit-test it by loading a table with some known data, and then querying to ensure that the data you expected got loaded into the table. Generating e-mails might be tougher to automate, but there are probably ways of doing that. Generating a report might be unit testable - but if the report is really just a human-readable presentation of data, that probably can't be unit tested very easily. Other sources of unit tests are past bugs - sometimes if I get a bug report, I try to create a unit test that breaks, exposing the bug. Then I have a good idea that the bug is fixed when the unit test passes. Unit tests are a great aid to testing and development, but of course there are not for everything. Things that can't be unit tested very well (e.g., your GUI) still need to be tested, just by other means. The generation of random numbers is too important to be left to chance.
-
Well, I try to unit test anything that is not trivial and has a well-defined result such that a test can be automated. For functions that load tables, you might be able to unit-test it by loading a table with some known data, and then querying to ensure that the data you expected got loaded into the table. Generating e-mails might be tougher to automate, but there are probably ways of doing that. Generating a report might be unit testable - but if the report is really just a human-readable presentation of data, that probably can't be unit tested very easily. Other sources of unit tests are past bugs - sometimes if I get a bug report, I try to create a unit test that breaks, exposing the bug. Then I have a good idea that the bug is fixed when the unit test passes. Unit tests are a great aid to testing and development, but of course there are not for everything. Things that can't be unit tested very well (e.g., your GUI) still need to be tested, just by other means. The generation of random numbers is too important to be left to chance.
-
Hey everyone, I have a few small questions on unit testing. I am kind of doing this backwards because my team is starting to make everyone do unit tests on there code and they want me to start with code that I have already written. So I am looking through my code and not sure what functions to unit test. I know you dont usually unit test GUI, or set and get functions. I have functions that load tables, generate reports and emails. Should I also unit test these functions? Thanks for all of your advice! :-D
There are 10 kinds of people in this world. Those who understand binary and those who don't. We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.
Maybe my article on writing your First Unit Test[^] will help. :) Marc MyXaml Advanced Unit Testing YAPO
-
Maybe my article on writing your First Unit Test[^] will help. :) Marc MyXaml Advanced Unit Testing YAPO
That is what I am looking for. I am going to read it now. I knew you would have something for me Marc....:laugh:
There are 10 kinds of people in this world. Those who understand binary and those who don't. We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.
-
Hey everyone, I have a few small questions on unit testing. I am kind of doing this backwards because my team is starting to make everyone do unit tests on there code and they want me to start with code that I have already written. So I am looking through my code and not sure what functions to unit test. I know you dont usually unit test GUI, or set and get functions. I have functions that load tables, generate reports and emails. Should I also unit test these functions? Thanks for all of your advice! :-D
There are 10 kinds of people in this world. Those who understand binary and those who don't. We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.
One thing I'd also suggest is taking a look at using Mock objects, they provide a nice way of letting you test interaction between objects that you may or may not be able to test 'reliably'. For instance, say part of your code needs to send emails out, but there's no guarantee that your email server will be alive/accessible to all developers when they're running the unit tests, or to the build/integration server. So, you build your EmailSender type around an interface that you can then call Send upon in your code, with mocks you can dynamically create objects that implement the interface, you can then tell your own code to use the interface on the mock to send the email, and you then test that the mock received the intended call, with the expected parameters etc. It also makes your unit tests nice and quick :) There are a few mock frameworks around for .NET (I don't know what language/libraries you're using), but I'm sure if you were interested specialists in other areas would be able to suggest something. For .NET, check out: http://www.nmock.org/[^] http://msdn.microsoft.com/msdnmag/issues/04/10/NMock/default.aspx[^] and for a more general treatment: http://www.martinfowler.com/articles/mocksArentStubs.html[^] Forgot to mention that although mocks are very useful in the example I mentioned, they also serve a nice way of testing interaction between even non-expensive/trivial types. -- Paul MS Messenger: paul -at- oobaloo-co-uk