views:

690

answers:

11

I manage a rather large application (50k+ lines of code) by myself, and it manages some rather critical business actions. To describe the program simple, I would say it's a fancy UI with the ability to display and change data from the database, and it's managing around 1,000 rental units, and about 3k tenants and all the finances.

When I make changes, because it's so large of a code base, I sometimes break something somewhere else. I typically test it by going though the stuff I changed at the functional level (i.e. I run the program and work through the UI), but I can't test for every situation. That is why I want to get started with unit testing.

However, this isn't a true, three tier program with a database tier, a business tier, and a UI tier. A lot of the business logic is performed in the UI classes, and many things are done on events. To complicate things, everything is database driven, and I've not seen (so far) good suggestions on how to unit test database interactions.

How would be a good way to get started with unit testing for this application. Keep in mind. I've never done unit testing or TDD before. Should I rewrite it to remove the business logic from the UI classes (a lot of work)? Or is there a better way?

+6  A: 

One option is this -- every time a bug comes up, write a test to help you find the bug and solve the problem. Make it such that the test will pass when the bug is fixed. Then, once the bug is resolved you have a tool that'll help you detect future changes that might impact the chunk of code you just fixed. Over time your test coverage will improve, and you can run your ever-growing test suite any time you make a potentially far-reaching change.

Jim Kiley
This is always a good idea, but this won't help testing odd interactions that might be unexpected.
Ben S
+6  A: 

TDD implies that you build (and run) unit tests as you go along. For what you are trying to do - add unit tests after the fact - you may consider using something like Typemock (a commercial product). Also, you may have built a system that does not lend itself to be unit tested, and in this case some (or a lot) of refactoring may be in order.

Otávio Décio
+17  A: 

I would start by using some tool that would test the application through the UI. There are a number of tools that can be used to create test scripts that simulate the user clicking through the application.

I would also suggest that you start adding unit tests as you add pieces of new functionality. It is time consuming to create complete coverage once the appliction is developed, but if you do it incrementally then you distribute the effort.

We test database interactions by having a separate database that is used just for unit tests. In that way we have a static and controllable dataset so that requests and responses can be guaranteed. We then create c# code to simulate various scenarios. We use nUnit for this.

yamspog
+1 adding unit tests as you add functionality
Jim Kiley
+1 to the unit testing as you add functionality, but I think when adding new functionality, you should be opportunistically refactoring the old untested code and adding new tests for the refactored bits as you go. That can also provide an "entry point" for adding more tests as you go and incrementally improving your test coverage.
Pete
+3  A: 

Refactoring IS the better way. Even though the process is daunting you should definitely separate the presentation and business logic. You will not be able to write good unit tests against your biz logic until you make that separation. It's as simple as that.

In the refactoring process you will likely find bugs that you didn't even know existed and, by the end, be a much better programmer!

Also, once you refactor your code you'll notice that testing your db interactions will become much easier. You will be able write tests that perform actions like: "add new tenant" which will involve creating a mock tenant object and saving "him" to the db. For you next test you would write "GetTenant" and try and get that tenant that you just created from the db and into your in-memory representation... Then compare your first and second tenant to make sure all fields match values. Etc. etc.

Paul Sasik
A: 

I think it is always a good idea to separate your business logic from UI. There several benefits to this including easier unit testing and expandability. You might also want to refer to pattern based programming. Here is a link http://en.wikipedia.org/wiki/Design_pattern_(computer_science) that will help you understand design patterns.

One thing you could do for now, is within your UI classes isolate all the business logic and different business bases functions and than within each UI constructor or page_load have unit test calls that test each of the business functions. For improved readability you could apply #region tag around the business functions.

For your long term benefit, you should study design patterns. Pick a pattern that suits your project needs and redo your project using the design pattern.

Nertim
One design pattern is not going to fit an application this large, plus force-fitting a design patter where it isn't needed is considered an anti-pattern. Something novices do while they're still learning. Design patters are used in this application where needed.
Malfist
A: 

It depends on the language you are using. But in general start with a simple testing class that uses some made up data(but still something 'real') to test your code with. Make it simulate what would happen in the app. If you are making a change in a particular part of the app write something that works before you change the code. Now since you have already written the code getting testing up is going to be quite a challenge when trying to test the entire app. I would suggest start small. But now as you write code, write unit testing first then write your code. You might also considering refactoring but I would weigh the costs of refactoring vs rewriting a little as you go unit testing along the way.

Shadow
+5  A: 

First, I would recommend reading a good book about unit testing, like The Art Of Unit Testing. In your case, it's a little late to perform Test Driven Development on your existing code, but if you want to write your unit tests around it, then here's what I would recommend:

  1. Isolate the code you want to test into code libraries (if they're not already in libraries).
  2. Write out the most common Use Case scenarios and translate them to an application that uses your code libraries.
  3. Make sure your test program works as you expect it to.
  4. Convert your test program into unit tests using a testing framework.
  5. Get the green light. If not, then your unit tests are faulty (assuming your code libraries work) and you should do some debugging.
  6. Increase the code and scenario coverage of your unit tests: What if you entered unexpected results?
  7. Get the green light again. If the unit test fails, then it's likely that your code library does not support the extended scenario coverage, so it's refactoring time!

And for new code, I would suggest you try it using Test Driven Development.

Good luck (you'll need it!)

Prutswonder
+1 for Art of Unit Testing - good book!
TrueWill
+7  A: 

I'd highly recommend reading the article Working Effectively With Legacy Code. It describes a workable strategy for what you're trying to accomplish.

Sean
+1 But better buy the book.
starblue
+5  A: 

I'd recommend picking up the book Working Effectively with Legacy Code by Michael Feathers. This will show you many techniques for gradually increasing the test coverage in your codebase (and improving the design along the way).

TrueWill
A: 

There's no better way to get started unit testing than to try it - it doesn't take long, it's fun and addictive. But only if you're working on testable code.

However, if you try to learn unit testing by fixing an application like the one you've described all at once, you'll probably get frustrated and discouraged - and there's a good chance you'll just think unit testing is a waste of time.

I recommend downloading a unit testing framework, such as NUnit or XUnit.Net. Most of these frameworks have online documentation that provides a brief introduction, like the NUnit Quick Start. Read that, then choose a simple, self-contained class that:

  • Has few or no dependencies on other classes - at least not on complex classes.
  • Has some behavior: a simple container with a bunch of properties won't really show you much about unit testing.

Try writing some tests to get good coverage on that class, then compile and run the tests.

Once you get the hang of that, start looking for opportunities to refactor your existing code, especially when adding new features or fixing bugs. When those refactorings lead to classes that meet the criteria above, write some tests for them. Once you get used to it, you can start by writing tests.

Jeff Sternal
A: 

I haven't tried adding test for legacy applications since it is really a difficult chore. If you are planning to move some of the business logic out of the UI and in a separate layer, You may add your initial Test units here(refactoring and TDD). Doing so will give you an introduction for creating unit test for your system. It is really a lot of work but I guess it is the best place to start. Since it is a database driven application, I suggest that you use some mocking tools and DBunit tools while creating your test to simulate the database related issues.

Hanseh