jsUnit vs jsUnit
If, like me, you’ve been confused by the two different, identically-named, unit testing frameworks for javascript, here’s a very useful explanation of their strengths, weaknesses, and purposes.
Matt Wynne taking it one tea at a time
If, like me, you’ve been confused by the two different, identically-named, unit testing frameworks for javascript, here’s a very useful explanation of their strengths, weaknesses, and purposes.
My notes on DanNorth and JoeWalnes‘ session at Spa 2008.
Five artefacts:
Four roles. People might fill more than one, or more than one person might be in a role:
Taking a requirement, the Stakeholder and the Analyst have a conversation:
Then the Analyst and the Tester have a conversation:
Tester then ‘monkeyfies’ the scenarios, using the following template:
Given … - assumptions, context in which the scenario occurs.
When … - user action, interaction with the system
Then … - expected outcome
e.g. Given we have an account holder Joe and their current account contains $100 and the interest rate is 10% When Joe withdraws $120 Then Joe’s balance should be $-22
The tester and the developer sit down and write an automated test to implement each scenario.
You might chain these up, but you can always categorise test code into these three partitions. This really helps how you look at test code.
See the Consumer Driven Contracts paper on Martin Fowler‘s website.
Consider extending / creating the domain model to cover the application itself – the UI, the back end.
Loads of tools are availlable. Use whatever works and build on it.
Ubiquitous Language – Start with a shared language. It becomes ubiquitous when it appears everywhere – documents, code, databases, conversations.
You will use different vocabularies in different bounded contexts. A context might be your problem domain, testing domain, software domain, or the user interface domain.
Beware which roles understand you when you’re talking in a particular domain. Often terms will span domains.
e.g. NHibernateCustomerRepository <– 1 –><– 2–><– 3 –>
1 = 3rd Party Provider Domain 2 = Problem Domain 3 – Software Domain
Make your tests tell a story – make it flow. Don’t hide away things in Setup methods that will make the test hard to read. If that means a little bit of duplication, so be it. ‘Damp not DRY’.
Basically what you need is a way to assemble different permutations and combinations of Given / When / Then with different parameters to make different scenarios.
Think in terms of narrative, flow. Think in terms of bounded contexts, and who the audience (role) is for that context. Who will understand that vocabulary?
Make sure the intent is clear – that’s the main thing.
Do you want to hook into continuous integration build?
Which version of the code is it going to run against?
Keep the tests in two buckets: * in progress * done
Those which are in the ‘done’ bucket, should always work, those which are in progress are allowed to be failing, until you make them pass.
Things you can do today.
What to aim for.
As with most stupid questions like this, the answer is “neither”. There are times when integration tests really help, and there are times when they can be a pain in the neck.
I was prompted to write this post when a colleague pointed me towards this page on the behaviour-driven wiki, which mentions the disadvantages of integration tests, which usually involve some complex (and often slow to run) procedure to set up an expected state in the system your code is integrating with. This tight coupling with the external system reduces agility and makes the test code brittle.
The page does point out that “even in this state the code is often much more robust and a much better functional fit than code developed under more traditional methods based on large-scale up-front design”
I agree with the principles in the article, and I believe BDD is a great way to think, but I do think as long as the integration tests are well-factored (and hence easy to change) then the problems highlighted don’t apply – you’re still going to be quick on your feet if requirements change.
The question is whether you’re going to spend more time fixing your unit tests than you would debugging the code – if you’re confident you can write it correctly first time and anybody needing to change it is highly unlikely to introduce bugs in the area you’re coding, it’s a waste of everybody’s time to write a unit test – the test just becomes baggage for the team to drag around.
Conversely, if there’s a risk that future changes could break what you’re coding, or you’re bored of hitting F5 in the browser to test some subtle tweak in a function way down deep in a subsystem, thinking of an imaginative way to write a lightweight unit test that isolates that function and proves that it works as you want it to, is probably going to save you some dull debugging time.