Skip to main content

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.

What I do mostly?
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 cool things:
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.

The benefits:
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:
  • It could run
  • It would eat
Making it an unicorn will not change its behaviour rather will add another:
  • It will fly
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.

 Example:

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)

Comments

Popular posts from this blog

Smart wifi controlled irrigation system using Sonoff and Home Assistant on Raspberry Pi - Part 1

If you have a backyard just for the sake of having one or it came with the house and you hate watering your garden or lawn/backyard then you have come to the right place. I genuinely believe that it is a waste of my valuable time. I would rather watch bachelorette on TV than go outside, turn on tap, hold garden hose in hand to water. Too much work!! Luckily, we have things like sprinkler system, soaker etc which makes things a bit easy. But you still have to get off that comfy couch and turn on tap (then turn off if there's no tap timer in place). ** Skip to the youtube video part if reading is not your thing   When I first moved into my house at first it was exciting to get a backyard (decent size), but soon that turned on annoyance when it came down maintaining it, specially the watering part. I laid bunch sprinklers and soaker through out the yard and bought tap timer but I still needed to routinely turn on the tap timer. Eventually few days ago I had enough of this rub...

The story of a Hack Job

"So, you have hacked it" -- Few days ago one of the guys at work passed me this comment on a random discussion about something I built. I paused for a moment and pondered: Do I reply defending how that's not a hack. OR Do I just not bother I picked the second option for 2 reasons: It was late. It probably isn't worth defending the "hack vs" topic as the comment passed was out of context. So I chose the next best action and replied "Yep, sure did and it is working great.". I felt like Batman in the moment. In this post I will rant about the knowledge gap around hacking and then describe about one of the components of my home automation project (really, this is the main reason for this post) and use that as an example how hacking is cool and does not always mean bad. But first lets align on my definition of hacking: People use this term in good and bad, both ways. For example: "He/she did a hack job" -- Yeah, that probably...

Kubectl using SSH tunnel for TKG K8s Clusters

We know SSH'ing and probably many knows about SSH tunnel. The way, in my opinion, these 2 (SSH and SSH tunnel) are different to me (and I am in favor of SSH Tunnel) is how I use it. From tooling perspective I would almost always do tunnel instead of direct ssh.  In this post I will describe how to do SSH tunnel for kubectl to interact with remote kubernetes cluster (Specifically Tanzu Kubernetes Grid aka TKG cluster). Get the project ready to go from my github:  https://github.com/alinahid477/vsphere-with-tanzu-wizard Topics Backstory SSH tunnel for TKG Clusters using Docker container Technical stuff: Tunnel through Bastion for TKG K8s cluster Technical stuff: SSH Tunnel for Kubectl for remote K8s Clusters (same with or without docker) Technical stuff: Explain me this A famous quote from Darth Vader himself: "Feel the power of SSH Tunnel" Backstory Why ssh or ssh tunnel? The below diagram shows in what scenario a SSH or SSH Tunnel almost becomes a necessity. Let's st...

Understanding The Ingress and The Mesh components of Service Mesh

I wrote about the key concepts about service mesh and how to evaluate the requirements for a service mesh in my previous post here:  Deciphering the hype of Service Mesh . This post is a follow up from there covering the technical aspects. Part 1:   Deciphering the hype of Service Mesh Part 2:   Understanding The Ingress and The Mesh components of Service Mesh. Part 3: Uderstanding the observability component of Service Mesh (TBD in another post).  Almost all popular service mesh technologies/tools (eg: Istio, LinkerD) have both ingress and mesh capabilities. Conceptually, I see them as 2 mutually exclusive domain (integrated nicely by the underlying tool). Understanding  the ingress  and  the mesh  components individually, such as what they offer, what I can do with them etc, was the basic building block to my understanding of service mesh technology as a whole. This is arguably the most mis-represented topic in the internet. So, I thought,...

CastleWindsor issue with MVC Area

I have been stuck with this issue and couldn't take it out of my head. Hence, ended up putting in some heavy hours solving it. But hopefully it is worth it. THE CONTEXT: I am implementing a MVC solution for an existing Sitecore 8.0 implementation which uses Castle Windsor for it's dependency resolver. Let's say a a tiny microsite. I had to implement a SPEAK app as per one of the requirements. Below are the 2 most important things behind why I ran into this issue in the first place: I needed to call a WebApi from my SPEAK app. 2. I decided to take MVC Area approach for my "tiny microsite" on a completely different sets of dlls For example the dlls for my "tiny microsite" are MyTinyApp.Web.dll, MyTinyApp.Business.dll whereas the main website's dlls are BigWebsite.Web.dll, BigWebsite.Business.dll etc.  WHY MVC AREA: The reason I took the MVC Area approach was to completely separate my "tiny microsite" so that I don't ...

Hall of justice - Authorisation Greeting System

Ever since I watched the Young Justice EP-1 the security system of the Hall Of Justice and Mount Justice wow-ed me. After all it was built by Batman. You see similar AI driven voice guided system in pretty much in all sci-fi series these days. I always dreamed of having something similar of my own. Well, now I have it (sort of). Although we not quite in the flying cars era yet (disappointment) but IOT powered locks are somewhat normal these days. The adoption rate is great.  Some background: What is this Hall Of Justice Authorisation system? This is the security system that Batman built for Hall Of Justice. The movies haven't shown it yet but there're several scenes in the animated series and comic books. Basically, it is a AI powered voice guided intelligent security system that scans bio signatures (like retina, body dimensions, temperature, heart rate) through a scanning device and identifies which member of the justice league it is, logs entry then gr...