0

I have some BDD tests for my software, declared in Gherkin and run using Cucumber JVM. The Cucumber JVM tests could be run at any of several levels (layers) of my application: through the front-end (HTML using Testcontainers), through the back-end (JSON over HTTP through the REST API using Testcontainers), through the back-end in a (Spring Boot Test using Java method calls) test harness using a mock HTTP server, or (for some tests) through the service layer (Java method calls).

But of course I want to test all those layers of my application, to some extent. And that means I want to have some duplication of my BDD tests. I don't want to run all the BDD tests at all the levels. And I don't want to test only through the front-end, so it is easier to debug test failures. At some levels I want to do only a few key tests to show that the layers of the application are properly glued together.

If I naively implement some duplicate Cucumber JVM tests, Cucumber will complain about duplicate step definitions. How do I do duplicated tests, without having Cucumber be confused by duplicate step definitions?

This is a distinct problem from reusing step definitions: at different levels, the code for a step is very different. And it is distinct from testing variants of and application, where different build environments use different step definitions.

Raedwald
  • 46,613
  • 43
  • 151
  • 237

1 Answers1

1

In order to do this, you would have to implement your step definitions on multiple levels. So for a step that should operate on the UI in one test, but on the API in another; you'd need 2 step definitions.

If you group these step definitions into different files, you can then create different runners pointing to different "glue" classes (step definition files".

You can group the step definitions that can be shared among the different levels into one file that is used in all the runners.

That said, I wonder whether you'd need to test the same thing (even if only a subset) at multiple levels of your application? Think about what the value of each test is, and how that would change what you are trying to validate. For example: If a method gives different output on different input, this can be tested in a uni test. To test if that result is displayed correctly, that might be a test on UI or API level. If there is additional logic in the UI on how this is shown, that might be a test on UI level.

Marit
  • 2,399
  • 18
  • 27