Writing Unit Tests for Existing Code?
out-of-order asks: "I recently became a member of a large software organization which has placed me in the role of preparing the Unit Test effort for a component of software. Problem is that everything that I've read about Unit Testing pertains to 'test-driven' design, writing test cases first, etc. What if the opposite situation is true? This organization was writing code before I walked in the door and now I need to test it. What methodology is there for writing test cases for code that already exists?"
Hopefully, you've got well-defined interfaces to the various modules/objects/subroutines within the code (if not, what exactly are you intending going to apply Unit Tests to?). Now, using only those specifications, design the tests you'd like the code to pass, exactly as if the code had not been written.
Now test it.
Before you actually fix it.
Also require all new code to have matching tests and setup automatic tests to slap developers who add code that doesn't get tested.
Good luck.
I don't see TestFirst as a Test Strategy, but as a design technique. Writing Tests first forces you to think differently about what you want to write.
This forces you to write testable code - writing tests afterwards does not force you to do that.
Of course, having the tests available later proves valuable for testing your application, but the tests main purpose is to lead you to a testable design
You'll most likely experience severe difficulties in adding Unit Tests to previously untested code. It might be easier to add acceptance tests (e.g. high-level scripts that utilize the application), especially if you want to cover more than small partitions of the application quickly
Write the tests as if the code _hadn't_ been written. Get the requirements and then write the tests from them.
Then if they fail the tests you'll have to discover if the requirements are wrong or if it's the code that's at fault. But at least you'll have something to start from - and you'll probably find some bugs they missed.
My Journal
You have to write tests as you change features. Lets say you have a simple change, tweaking a short method to do something different. First you write a test for its exisiting functionality, make sure it passes. Then add a test for the new functionality, run it and watch it fail. Make your change and make the test pass. This would also be the point where you can do some refactoring or clean up, or extend the test to catch boundy conditions.
1 177052/002-8698615-6720004?v=glance
With legacy code, you just have to start writing tests with the code as you go, writing tests for functionality that you need to understand or review. If you try and take x number of weeks to write test cases, your doomed to fall behind and have obsolete tests when you are dumb.
Also, see Working Effectively With Legacy Code by Michael Feathers --> http://www.amazon.com/exec/obidos/tg/detail/-/013
robsanheim.com
We use McCabe (http://www.mccabe.com/) to point us to problem code. Running their tool gives you a good idea of where the complexity of an application lies and where you should focus your testing.
It works kinda like this: First the tool parses everything and generates a ton of metrics. This will point you to the complex modules of the application. Then it breaks down each function/method in the module into its possible execution paths and turns this into a graph. By looking at the graph, you can see what you need to do to get your test to follow a particular path.
McCabe also tells you how many test cases you'll net to completely test a give block of code. It assumes, however, that all of the tests are independent, which is not always the case.
On the down side, McCabe is big, expensive, and sometimes returns unreliable data. But it's a lot better than nothing.
If you're looking for free tools, look for things that calculate "cyclomatic complexity". This is the measure of the number of paths through a given block of code. Armed with that, you should be able to make some headway.
Good luck!
I spent several years managing a test team in a Fortune 100 company, and I have seen this situation many times (it's probably the norm, rather than the exception, in industry today).
Let the documented requirements for the code (or product) be your guide. Use those requirements to develop test cases, then design one or more tests that hit all of the test cases.
If there are no documented requirements, then you should ask yourself why you are working there. This situation usually leads to many arguments about what the code/product is really suppose to do, and you'll just become frustrated while you waste lots of time. It's not worth it.
There is a book that covers this subject well: Working Effectively with Legacy Code by Michael Feathers.
http://www.amazon.com/exec/obidos/tg/detail/-/013When I worked at Cisco on a project that was written in Java we used an automated unit testing tool that would test each method and report on what would break , for instance if you passed a particular value to a method it would fail, maybe you should fix the code to deal with that possibility. It was either Junit or Jtest ( one of them costs $3000 a seat, we used that one. ) It was good thing since QA categorically refused to "test" the software by trying to break it, they would only tested to see if it could work if the customer did everything right.
Anyway, even if you aren't using Java you might look at the documentation of Junit for ideas about building test fixtures for components of your code.
Thankfuly I don't work there any more.
Such tools can make after-the-fact testing quite a bit easier.
:-)
We used automated regression testing scripts in the mainframe environment I worked in 12 years ago, and that made some aspects of unit testing relatively easy.
Unisys had a tool (TTS1100) which allowed us to record each online transaction entry and computer response and then play it back later, and that made it possible to perform the exact same tests dozens or hundreds of times if needed. We used to run them after each set of changes was applied to make sure nothing broke.
One could also record a single occurrence of a lengthy interactive sequence and then add things like variables and looping structures into the recorded script to automate the handling of various test cases using different values.
Such a tool makes after-the-fact test design a little bit easier because you can sit down and methodically address each and every variation of each and every input field on a given screen.
Of course, the nature of the software you're using might make that sort of thing more difficult, or perhaps even easier.
I've never been able to do up-front unit test design -- specifications can change rather quickly when doing in-house software development, and the overall environment is a lot more dynamic than a typical "software house" environment would be where one always has formal detailed product specs to code to. We're often writing code based on an e-mail or on a couple of phone conversations.
Mainframe/UNIX Bit Twiddler and long time Windows/Linux Hobbyist.
The Theorem Theorem: If If, Then Then.
Unit testing is a method you use to achive something. Is the current component very buggy and you need to rewrite it or do you need to extend production quality software without breaking existing functionality?
If you are testing a component try to figure out if it is possible to in some schematic way. If you can figure out a way for the "business" people to write the tests for you that will take a lot of knowledge off your shoulders.
If it is an existing component maybe you could explore if it is possible to make some mechanism that "records" actions to the component and then later be able to "replay" them and check if the results are the same with your new or changed component as the production quality one.
I recently started as a contractor on a J2EE project that has lots of problems. The application has a classic backend with lots of ugly EJB anti-patterns and everything. The frontend is a VB client that communicates via. a simple webservice.
In a couple of days I was able to make a regression test engine that can save the xml-communication that our business-clever testers make to the server and then at any given time later run the same requests to the server and check if the responens match the originally recorded ones.
It works wonders and I now have free hands to clear out a lot of the technical mess while always having proof that I havn't broken anything.
Think about what your goals are. Then find the best tool to get there.
Welcome, to the world of the Real.
In most IT shops, I'm sorry to say, test cases are a low priority and almost always come after the code is complete or nearly complete.
If you're looking for a "methodology" for creating unit test cases, I think you're overthinking the problem. You need to create a set of test caes that assure you that the unit is working in and of itself. There are a number of things you can do to accomplish that:
1. Look at the design document.
See what the design document says the unit is supposed to do. Write test cases to test the various aspects of what the unit is supposed to do. If it transforms input, write test cases that test expected input, boundary cases, completely invalid input, no input, and as much damn input as you can generate. Do that for every function of the unit.
2. Look at the code.
Because the design document may be outdate, incomplete, or just MIA, look at the code. Hopefully it's commented well and tells you what the unit should do. Write test cases that you are sufficient to exercise each function in the unit, and if possible each branch of code in the functions in the unit. Ideally every line of code gets executed at least once by your unit test case suide, though 100% coverage is not always possible or cost effective.
3. Look at other test cases
If Integration or System test cases have been written, then you may be able to use those to school on. Integration test cases will in particular show you what you don't need to bother testing at the unit level. It will also show you where your stubs and drivers need to be built. The system test cases may give you more ideas for functionality that needs to be tested.
Sorry that you've been dumped in this situation, but it's the same situation that many, many of us face every day. It is the unfortunate reality of software engineering at a lot of IT shops.
Gimme a fucking break.
Every testing job I've ever had we've had ZERO documentation. NADA. ZIP.
How do we survive? WE TEST. We put down the book (like we had one to begin with) and we test. Surely you have a server somewhere running dev-level code (at least) and you start poking around. Sure, its less than ideal, but you deal with it. And you bitch about how crappy it is and how it goes against all the principals of so-called 'real world' methodologies.
The thing is, this is how the real world does it.
Sure, in a perfect world, everyone has their shit in order. But in a perfect world we're not all competing against code monkeys working for 1/10th of what we make and that live in a 3rd world country.
Religion is for people afraid of going to hell.
As above, write your tests to the specs.
;-) if necessary.
Run the tests and document the results.
Let someone else mod the specs
If n.c's third paragraph applies, you either have to find a managerial ally who will support you as you re-work the design process and local culture to be a bit more rigorous and disciplined. It will be tough, but can also be rewarding.
Good judgement comes from experience, and experience comes from bad judgement.
- W. Wriston, former Citibank CEO
Trying to write a test case for all the code you have will be very difficult, very long and to be honest not buy you a lot.
:)
A few open source projects have found themselves in the same situation as you, and they seem to work by 3 rules:
1) If you change any code at all which doesn't have a test, add a test
2) If you find a bug, make sure you add a test that fails before, and works now
3) If you are ever wandering around trying to understand some code, then feel free to write some tests
One thing I will say is to try very hard to keep your tests organised. Keeping them in a very similar directory structure to the actual code is helpful. Without this it's very hard to tell what has and hasn't got a test.
Combination - fun iPhone puzzling
I inherited a 1000 class Java based toolkit from my predecessor, which had exactly zero unit tests. Over the last two years, we've made a sustained effort to employ Test-Driven Development and add more tests to ensure that everything works as advertised. As of today the toolkit has over 830 tests, with line coverage of 61% and class coverage of 96%. We've still got a long way to go, but were much better off than we were. Here's how we got there...
1) A lot of people are going to tell you that you need to write your tests from scratch. That you should assume that your code is broken and work out the expected results by hand and create the test assertions accordingly. I disagree. If you're testing old code, it's much more useful to use the test to ensure that it does whatever it did before, instead of ensuring that it's "correct". I prefer to treat the code as though it is correct, and build the tests around it. Even if the assumption is occasionaly wrong, you can make the tests much quicker this way. That allows you to refactor and extend your system with confidence, knowing that you haven't broken anything. Remember, TDD isn't really about quality assurance, it's about design and evolving design through refactoring. More tests == more refactoring == better system.
2) You're probably not going to get a lot of extra time to sit around and write tests. You need to captialize on the time that you have and turn problems into oppertunities to add tests. Whenever you find a bug, make a test that reproduces it. If you need to add supporting stub or mock objects, consider making them reusable so that future tests will be easier to write.
3) If you need to add new functionality to the system, just follow the standard TDD steps of Test->Code->Refactor, and make sure that you add tests for anything that might be affected by the change.
4) I'm assuming that you already have a continous integration build that runs the tests, but if you don't, make one. Now. Also consider adding other metrics to the build like code coverage (we use Emma), findbugs, and jdepend. These will help you track your progress and can be very useful if you have to defend your methdology to people who view TDD as a waste of time (The Code Coverage to Open Bugs ratio gets them every time).
5) In general, you need to look for oppertunities to write tests. Don't understand how a module works? Write a test for it. Found a JDK bug? Reproduce it with a test. Performance too slow? Use timestamps to ensure that the performance of a alrorithm is in a reasonable range.
You've probably got a long road ahead, but it's worth the work. Keep at it, and good luck.
Look through old records of bugs that have been fixed in the code (e.g. bug-tracker tickets), and write unit tests to detect that bug. Prioritize bugs that have shown up more than once. The idea here is that one doesn't want to have to diagnose and fix the same bugs twice.
The purpose of unit testing is to make sure that the unit works (and to characterize unit failures). It's the sanity check before you throw it 'over the wall' to the test and quality organization.
You want to do path coverage, statement coverage, bounds checking on the inputs, error conditions, that kind of stuff.
Pick up a book by Boris Beizer, read his stuff and ignore everyone else. I've been in QA and Test for almost twenty years now, Beizer is -the man- to read about testing. If you're really desperate send me a message here and I'll send you a template to work from.
Guys, my legacy code doesn't have functions, just VB subroutines that modify global variables. Any idea how to make unit tests for this? And by the way, the functions aren't cohesive, each one is 100's of lines and does different sometimes unrelated things.
Something about a million monkeys sitting at a million keyboards comes to mine :)
As long as all the tests pass, there's really no harm in testing anything you want. Nobody's ever going to tell you to write less tests. Just remember to do breadth first and get something written for all the obvious spots, rather than getting all trivial and obscure on your favorite class just because you can.
The developer excuse is almost always "I don't have the time/energy/patience to write the unit tests for this legacy code." So what you want to do is provide a foot in the door that allows the developer to realize that maybe he is really only updating existing tests, and possible creating a few, but not all of them. Much less work.
But then again I may have misunderstood your question.
www.HearMySoulSpeak.com
Often there aren't detailed enough requirements.
With requirements at a typical business level, you could have X totally different systems that meet them (and most better given hindsight). And often that level is as much as you're going to get when the original team has left.
Anyway recreating requirements at a detailed technical level could be a waste of time - because some module could be required to do something stupid by another module. Once you fix things all round, this requirement will be thrown out.
At one of my workplaces I made major changes in behaviour of some modules - e.g. instead of N^2 it's just N. And some things I just threw out because they were redundant.
I suppose, you could rewrite the requirements (after figuring things out), and then rewrite the code. But that's quite different from _getting_ the requirements.
1. Choose a testing framework. It depends on language. For C/C++ - cppunit, for Java - junit.
2. Start writing unit tests from lowest level functions: those that use barebones system libraries and start moving upwards. Do not go too far - it's a unit (!) testing, not a general or rgeression testing.
3. Ideally you should test all possible paths in functions. Apparently it is not feasible for large functions (that is one of the reasons function should be small). You should try to create a test to hit every condition and every branch of a condition at least once.
4. It's more complex that these 3 items, but I'd say this is a start.
my sstream of consciousness
Some people are recommending that you treat the modules as "black boxes" and write the tests according to their specs. Problem is (as others have pointed out) the specs are not detailed enough. So you will inevitably wind up looking at the code and then writing tests that prove the code does what it says it does.
And this is actually OK, because it simply means that the "Unit test creation" process is actually a detailed code review process. Expect to find far more bugs from looking at the code than from running your tests. But this doesn't matter: the bugs have been found.
Don't expect to be able to reuse the tests after modifying the code because any change to the code will generally break the test.
You might have a look at QuickCheck though. It was written as a unit test system for Haskell functions: you write a formal spec of the properties of the module and it generates random tests according to that spec. The latest prototype (see the website pointed to by www.haskell.org) also handles modules with state, and there is no reason why it couldn't be used for general software test. By organising your test around formally specified properties of modules instead of individual procedures you should get more reusable tests.
Paul.
You are lost in a twisty maze of little standards, all different.
Try to get your hands on Working effectively with legacy code by Michael Feathers.
First of all you need to decide what you want to achieve from your testing. If you're following a methodology such as the 'V'-cycle, then the point of unit testing is to verify that the code correctly implements the design (system testing is where you check that the system implements the requirements).
...) and it could be argued that this kind of testing is almost pointless - it would be far more productive to concentrate on system testing.
Many replies here are along the lines of "we don't have any documentation" - well in that case, you can't do truely meaningful testing. You can test what you think the code should do, but that'll always be based on assumptions (and we all know about those
Of course, there's nothing to stop you ammending/clarifying/extending any documentation that you do have while in the unit testing process, as long as you take due care and follow your prescribed process.
Finally, if I may offer some advice - document (and apply QA) your unit tests. In many places there seems to be an attitude that "they're only tests - they don't need good documentation", which in turn creates a maintenance nightmare in itself.
A recent article in print about automated unit tests for legacy code was
"Managing That Millstone"1 c/sdm0501c.html
By Michael Feathers
Software Development
January 2005
http://www.sdmagazine.com/documents/s=9472/sdm050
It included suggestions for how to inject unit tests into code which isn't loosely coupled, some tips on how to refactor to get loosely coupled interfaces, & what you can do when neither of those approaches will work. It was a valuable & enjoyable read for me, at least.
gene
I too have had good luck with Working Effectively With Legacy Code. Highly recommended.
My
Others have touched on this, but you shouldn't be looking to write unit tests just to say, "Hey, I've tested some of this code." Wait till you have to change something or add a new feature, then focus your energy on writing tests in those areas you need to protect. Then make your changes.
Unit testing is for finding bugs early on (preferably design errors, but also coding errors).
If the code is already written and works, then it's not likely to be worth the effort to add random unit tests all over the place. What you need then is either (a) stress testing, to discover hidden bugs, or (b) regression tests, to make sure the software keeps working, even after programmers have "improved" upon it.
Even worth learning ADL (:-))
--dave
davecb@spamcop.net
If the product already exists, then you know what it is supposed to do. All you have to do is come up with scenerios to test what it does. You should already have a users guide, so you basically go to your users guide and look to see what it says its supposed to do. Then start testing the guide.
Only 'flamers' flame!
Does slashdot hate my posts?
You are so screwed. Writing tests for untested code is a thankless job. You are going to find so many bugs, and everyone is going to get really pissed off about that new hire that is rocking the boat complaining about "quality problems".
You are in a no win situation. They will tell you your tests are too picky, that no one will use it like that. Unit testing is thankless, you can't argue. Given that there was no test plan, I bet there isn't even a spec! Where there is smoke there is fire.
I'd start looking for a new job right away.
"This mission is too important to allow you to jeopardize it." -- HAL
...By Michael Feathers. The scenario that you may find is "we can't refactor until we have unit tests and we can't have unit tests until we refactor". The book has some strategies for getting around that paradox. You may find, however, that some code is essentially un-testable as written.
Presumably since these components are all built, they have some idea of what they are supposed to do, and some sense of parameters.
Make stubs or some other kind of testing tools, hammer data into them, examine the data coming out.
I guess I don't quite follow the question. You're actually in a better position with already-developed code, for the simple reason that if these things are already developed, they already have a defined purpose (whether that was defined before or after the fact). Your only problem will be to figure out how to separate the components and find ways to interface with them.
Terrorists can attack freedom, but only Congress can destroy it.
And guess what, unit tests are very much a part of a good regression test suite... So writing unit tests are most certainly not a waste of time even if the software is "working". Besides I've lost count of the number of times I've found serious errors in "working" code when adding unit tests.
Dude, you just pretty much defined all QA everywhere. The only thing you missed was the part about being crammed in at the end of the release schedule after development was days late on their end and being expected not to let the date slip.
Terrorists can attack freedom, but only Congress can destroy it.
First of all, don't panic, you're not alone! I have done this a few times already. Here's my approach:
1. Identify "enduring business themes" in the code. This means basically a group of code that can be predictably tested by feeding certain input and expecting certain output. For example, you know that if you order two pens, a purchase order for two pens will come out the other end.
2. Once you establish a few of these scenarios you can write a few high-level unit tests. These will help you acertain whether or not this code works ok.
3. Once you have this, you're on your way to having test driven development in place. When any code changes have been done, quickly run your unit test to ensure results are still ok. If not, it means code change broke something (test driven quality assurance).
4. Now you can dig deeper and make more detailer fine grained unit tests.
Most times the best you'll be able to do is to lock in existing code base with a handful of solid high level unit tests and ensure that any new code is tested properly.
You're better off writing integration tests and system tests - much more bang for your testing buck.