Behavior Driven Test Cases
I have read a bit on BDD. But none of the theory seemed appealing enough that I would be like âhai ! this is cool. Letâs do thisâ.
Probably because I am a lazy tester or more appropriately test case writer (unit test that is J).
 What supposed to happen ?
TDD. Write test cases first Ă drive the development from there ( based on whatever model is chosen or appropriate).
 What actually happens ?
As much as the next guy wants to throw theory and technical jargons Iâm not really sure how many mid-scale project processes actually follow TDD throughout the entire development phase, let alone maintenance phase(where itâs most important) OR BDD for that matter.
The truth is (at least if I speak for myself) I have not followed through a proper TDD process. Yes there always exist some test cases that are written and thrown into build process so that we can use it in future.
Even more sometimes we write the test cases after the feature is developed.
But I donât think thatâs TDD.
 My reasons for why I am reluctant ( sort of ):
Writing test cases is difficult (in terms of extra work).
Usually the attitude is like âI have written the feature and I have executed the code over and over until I got it working. So itâs testedâ. Which is fine.
But what about when we change / develop some related feature and want to make sure whether our previously developed feature is still working fine ?
Or the other guy did not break things collectively ?
Yeah! Manually testing it over and over is lame ( even though I do it pretty much on a daily/regular basis ).!
Maintaining test cases is even more difficult.
For example: we started making a horse as our feature. And is done and tested. Then client said they want an unicorn. But along the way of putting the âhornâ and âwingsâ on the âhorseâ resulted in changing the some basic anatomy about the âhorseâ like âit can freaking fly nowâ. So our old test cases will fail (well at least mostly). And I wouldnât want to write or modify them is again (or at least will feel like my hands are heavy).
The theory of âBDDâ and âBDD vs TDDâ confused me big time. And of course them cowboys put their opinions the way they found it good (which obviously is conflicting).
But the cool thing I have found is a tool called cucumber (https://cucumber.io/). Cucumber is awesome.
AND using cucumber to write test cases will fit in just fine with my laziness to write test cases. I have made peace with myself that I am not going to do BDD or TDD but I will write test cases.
From now on I will focus on test cases written using cucumber (thus somewhat bdd).
Lets take the example of âthe client is turning the horse into an unicornâ.
So the behaviour of the horse was:
Making it an unicorn will not change its behaviour rather will add another:
So we will only have to write 1 more test method to test flight. And as it is âtest case written in cucumber which focuses on behaviourâ we wonât have to refactor our test methods because we refactored our code.
 The other cool things are:
I can literally copy-paste user stories to write my test cases ( because cucumber is awesome)
Itâs English language. That means I can read through the test cases and understand what one is trying to test (even though my only skill is writng emails)
Test method are variable driven (thanks to cucumber).
Thus I can avoid writing bunch of Mocks which can and usually does become absolutely useless at later point.
Hence, I am avoiding âhard codingâ things.
The âtest resultsâ are descriptive so it can be used ( as DFO ) as output of tests for clients.
 Ofcourse the solution needs to be designed properly. Otherwise itâs a lost cause anyway.
Source code : https://bitbucket.org/ali_nahid/kitchensinkbdd/overview
This is how my âserviceâ test cases looks like:
So when I refactored my service layer to use âcomman patternâ I did not have to rewrite any of my test cases at all.
This is also replacing Mock object.
As my âTestâ project contains its own config and because I am using âcode firstâ I can create/update or change datasourse ( here in actual application I am using SQL Server but for test I am using âlocal storageâ).
 This is the âtest resultsâ look like:
 In summary I can be lazy and still write test cases so that I can keep using them in maintenance phase possibly without writing anything on test method.
This is the pattern I followed ( but itâs a little bit weird as Read part is also in same the repo as the write .. but this example project was simple enough)