Search code examples
javatestingjunitcucumber

What type of cases should be covered under Junits and which ones under Cucumber


The Developers are usually writing test cases with Junits

The Testers are usually writing test cases with Cucumber

I am confused, that how are these(Cucumber & Junit) different, if at the end, both are meant for validating logic of our code !

Is my supposition correct, if I say...

  • simple method testing is done by Junits

  • tough scenarios, we have Cucumber


Solution

  • Unit tests, as their name suggests, are designed to test small units of code. A unit test is written in code, this makes it less readable to the business, but also makes it very powerful and potentially very fast.

    Cucumber is a tool used to describe behaviour. Each cuke will exercise a much larger chunk of code.

    The two sorts of tests are very different. So different that most Cucumber enthusiasts don't want to use the word test (https://cucumber.io/blog/collaboration/the-worlds-most-misunderstood-collaboration-tool/)

    Scenarios are not at all about validating the logic of your code, they have nothing to do with code. They are about describing the behaviour of your application and supporting the development of that behaviour. After the scenario and the behaviour has been implemented they are used to confirm that the behaviour is 'probably' still working as intended.

    Unit tests are all about code, they are written in code and (ideally) connect directly to code. They definitely can be used to validate the logic of your code.

    There are some other major differences

    Runtime

    • Scenarios are slow
    • Unit tests are fast (unless they are badly written)

    This is not a small difference. An optimally written unit test can be 1000 times faster than an optimally written scenario.

    Expressiveness and Power

    • scenarios use natural language to express themselves. They use abstraction and naming to wield power. Well written cukes are very readable by the business

    • unit tests use code to express themselves and to wield power. They are much more powerful. However its much harder for them to express themselves. Unit tests cannot be read by their stakeholders. Even the output of beautifully written unit tests is very hard to read for a non-coder

    Summary

    You have two very different things, working in different ways, using different tools (natural language vs code). So its not surprising you have different tools.

    Unfortunately we have lots of people using Cucumber as a test tool to write unit like tests, and we have lots of people using unit test tools to write slow tests that test large chunks of code. Both these things are possible, maybe even viable, but they are far from optimal.