testalicious testing

3 11 2008

Test Mashup, TDD / BDD

So these days test driven or behavior driven development are hot around my coordinates. Im going to write a bit about pratical usage of BDD using selenium & jBehave. With backup tools such as maven, cobertura and selenium.

We all hate to maintain large code bases of tests, tests are nice ( and necessary ) but sometimes you just get cluttered by them.

Introducing jBehave

So what if you could have a minimal amount of code and write something like this:

When user goes to homepage
homepage are rendered

Taking it a bit further:

Given a user nino with password dummy
When user goes to homepage and does a login
Then user are logged in and shown profilepage

That would be nice right? In jBehave you archive this by a blend of things, i’ll only cover a subset.

In your steps class you something like the below code, notice that I put in selenium. So the steps are sort of a mini test framework which the scenarios can utilize.

@When("a user visits $page")
public void visits(String page) {
selenium.open("http://localhost:8080/" + page);
@Then("$page are rendered")
public void seesPage(String page) {
"http://localhost:8080/" + page) == 0);
Ensure.ensureThat(!selenium.isTextPresent("HTTP ERROR: 404"));

The scenario are a plain text test, which utilizes the steps above. Something like this:

When a user visits homepage
Then home are rendered
When a user visits errorpage
Then errorpage are rendered

As you can see you could actually put in use cases, and things suddenly got a lot easier to discuss with customers, your tests are readable by non technical eyes and of course it’s not limited to web development. And your maintenance code base got smaller since what you need to write in java have shrunken, you are only building a small framework for the scenarios to utilize. Really really cool!

As you can see jBehave fit’s both for unit testing and the more obvious integration testing.

Introducing Cobertura

While you test it’s still nice to know your code coverage, cobertura’s nice for this as it gives you simple readable reports and it’s transparent to use. Just include a small mojo in your parent pom:

In your parent pom


And in the project pom using it





You will then get reports looking like this:

Cobertura report

Cobertura report

And you can even see which lines are touched in your code:

See what lines are touched by tests

See what lines are touched by tests

Introducing Selenium

Automatic independent browser tests, if your doing web development this is almost a must have, unless you like to do manual tests a lot. However if you are using code coverage which are using byte code instrumentation, you could loose track of what’s tested and what not. A way around this are perhaps to start a embedded jetty server as part of your test, then in theory you should be back on track. Since byte code instrumentation should be able to track again…




One response

24 01 2013

I’m not sure where you’re getting your information, but good topic.
I needs to spend some time learning more or understanding more.
Thanks for excellent info I was looking for this information for my mission.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: