01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

drive your code with TDD<br />

In TDD, tests DRIVE your implementation<br />

Now you’ve got a working and tested OrderInformation class. And, because <strong>of</strong> the<br />

latest test, you’ve got getters and setters that all work, too. In fact, the things you put in<br />

the class were completely driven by your tests.<br />

Test-driven development is different from just test-first development in that it drives your<br />

implementation all the way through development. By writing your tests before your<br />

code, you have to focus on the functionality right away. What exactly is the code you’re<br />

about to write actually supposed to do?<br />

To help keep your tests manageable and effective, there are some good habits to get into:<br />

1<br />

2<br />

3<br />

286 Chapter 8<br />

Each test should verify ONLY ONE THING<br />

To keep your tests straightforward and focused on what you<br />

need to implement, try to make each test only test one thing. In<br />

the Starbuzz system, each test is a method on our test class. So<br />

testCreateOrderInformation() is an example <strong>of</strong> a test that<br />

only checks one thing: all it does is test creating a new order object.<br />

The next test, which tests multiple methods, still tests only one piece <strong>of</strong><br />

functionality: that the order stores the right information within it.<br />

AVOID DUPLICATE test code<br />

You should try to avoid duplicated test code just like you’d try to avoid<br />

duplicated production code. Some testing frameworks have setup and<br />

teardown methods that let you consolidate code common to all your<br />

tests, and you should use those liberally. You also may need to mock up<br />

test objects—we’ll talk more about how to do that later in this chapter.<br />

Keep your tests in a MIRROR DIRECTORY <strong>of</strong><br />

your source code<br />

Once you start using TDD on your project, you’ll write tons <strong>of</strong><br />

tests. To help keep things organized, keep the tests in a separate<br />

directory (usually called test/) at the same level as your source<br />

directory, and with the same directory structure. This helps avoid<br />

problems with languages that assume that directories map to<br />

package names (like Java) while keeping your tests cases out <strong>of</strong><br />

the way <strong>of</strong> your production code. This also makes things easier<br />

on your build files, too; all tests are in one place.<br />

Download at WoweBook.Com<br />

Suppose you need a<br />

database connection: you<br />

could set that up in your<br />

setup() method, and<br />

release the connection in<br />

your teardown() method<br />

<strong>of</strong> your test framework.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!