Skip to main content

RestFixture

So most of the tests I'm writing now in Fitnesse are using RestFixture. Being able to do all this black box style testing has helped me get a lot of tests up and running without having to change the existing code base. Now I've taken a step future with my own little fork so I can use scenarios and build nice BDD style scripts. But first I want to give me own quick guide to using RestFixture

Step 1: Installing
You can dive straight in by grabbing the latest jar files for RestFixture here https://github.com/smartrics/RestFixture/downloads
If you know what you're doing can get the nodep version to work nicely along side other libraries you may be including in Fitnesse. But I grabbed the 'full' version and unzipped it into a RestFixture folder alongside my FitNesseRoot folder.
Step 2: Write your first test
I took advantage of the built in Fitnesse api as a basic test and wrote a page called RestFixture with the following contents
!define TEST_SYSTEM {slim}

!path RestFixture/lib/*.jar
!path RestFixture/RestFixture.jar

!define expectedReturnHeaders {Content-Length : [\d]+
Content-Type : text/xml }

|!-Table:smartrics.rest.fitnesse.fixture.RestFixture-! | http://localhost:8080|
|GET|/RestFixture?rss|200| ${expectedReturnHeaders} |//title[text()='RestFixture']|
If that runs and passes then congratulations! You've got a working RestFixture install! Step 3: Profit!


Pitfall 1: &
Stuff like /loadpreferences?session=9999&user=admin&module=config would fail and I was scratching my head. Now any good RESTful api designer will tell you that's a stupid url to be using and they'd be write. But it's someone's old mistake and I can either tell them how stupid they are or appreciate the fact it works and move on trying to get everything that's broken working too.
Why did this fail? Well because Fitnesse was turning my & into & and messing up the url with nothing to parse it back. Solution: Surround the url with !- -! to stop Fitnesse doing any parsing on it and it'll work just fine.
Pitfall 2: json + xpath
I was trying to figure out how to handle json that didn't confirm nicely to use xpath commands. Unless your json is wrapped in something like {node: stuff} then the xpath stuff just doesn't work. Something I will try and fix at some point. So i used a lot of the javascript parsing in there to access the jsonbody variable that would work just fine. So I got lots of things like this:
/* javascript */ jsonbody == false;
or this
/* javascript */ 
var match = null;
for (var i=0; i != jsonbody.length && match == null; ++i) {
  var node = jsonbody[i].map;
  if (node.name == 'Bob') {
    match = node;
  }
}
match != null;
or use the let ... js statements like this
response.jsonbody[4].block.name
Pitfall 3: Posting form like data
It's documented in later versions but not in 2.0 beta that you can use set body with url encoded data to behave like posted form data. Very handy. Although I'm still trying to figure out how this can work if combined with a file upload.

Comments

  1. Neat example at Step 1 - I just have "reused" it in the README. Thanks

    ReplyDelete
  2. You're welcome. It was literally the first test I wrote once I downloaded the jars :)

    ReplyDelete
  3. thanks for posting useful info on escaping the URL! The ampersand problem was bugging me for half the day today, and I was going to debug the source to see where it was happening. RestFixture with FitNesse is now cool again :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Setting up Fitnesse on Ubuntu in 7 steps

Some pretty basic steps but just to make sure it's here for everyone to see. Setting up fitnesse and running the jar is easy enough. Just go to http://fitnesse.org/ and get started and do it on your desktop just to see it in action. But for me that wasn't good enough I wanted it to run as service on ubuntu. I stole a few tricks from how ubuntu runs jenkins and setup fitnesse a similar way. 1. Create a user and group for fitnesse (optional) I didn't do this because I wanted tomcat, jenkins and fitnesse all running as the same user. Call it laziness to avoid any permissions classing but it doesn't change the process that you need to create or choose what user you're going to make it run as. Don't make it run as your user or root! 2. Download the jar file and place it in /usr/share/fitnesse Make the folder too of course. It can belong to root as long as the fitnesse user has read access 3. Create the folder to run in at /var/lib/fitnesse Fitnesse user needs...

Are mocks/fakes reusuable?

Programming 101 states: Don't copy and paste code. If you find yourself doing something repetitive then do it right so you can reuse the same code. Functions, classes and even separate files all serve this end. Now that I'm writing tests all the time I often find myself creating Mocks. Mocks are where you tell code to use a pretend version of some functionality instead of the real one. It could be because the real one does something you don't want in your tests (writes files, reads a database) or it could be that you've got some messy legacy code you can't to pull into your tests (yet). There's other reasons too but you get the idea. So if I make a Mock version of a class it makes sense to try and share that with everyone else that might be trying to test with that same class. Or does it? That assumption has some serious flaws that I'm only now starting to understand. And here's a few: Behaviour you need to test may be completely different to the next gu...