简体   繁体   English

使用黄瓜进行多级测试

[英]Multi-level testing using Cucumber

I have some BDD tests for my software, declared in Gherkin and run using Cucumber JVM. 我有一些针对我的软件的BDD测试,以Gherkin声明并使用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). Cucumber JVM测试可以在应用程序的多个级别(层)中的任何一个上运行:通过前端(使用Testcontainers的HTML),通过后端(使用Testcontainers的REST API通过HTTP进行JSON),通过后端-使用模拟HTTP服务器结束(使用Java方法调用的Spring Boot测试)测试工具,或在服务层(Java方法调用)进行(对于某些测试)。

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. 这意味着我对BDD测试进行一些重复。 I don't want to run all the BDD tests at all the levels. 我不想在所有级别上运行所有BDD测试。 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. 如果我天真的实现了一些重复的Cucumber JVM测试,则Cucumber将抱怨重复的步骤定义。 How do I do duplicated tests, without having Cucumber be confused by duplicate step definitions? 如何进行重复的测试,而不会让Cucumber被重复的步骤定义所混淆?

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. 而且,它与和应用程序的测试变体不同,后者在不同的构建环境中使用不同的步骤定义。

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; 因此,对于应该在一个测试中对UI进行操作但在另一个测试中对API进行操作的步骤; you'd need 2 step definitions. 您需要2个步骤定义。

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. 例如:如果一种方法在不同的输入上提供不同的输出,则可以在uni测试中对其进行测试。 To test if that result is displayed correctly, that might be a test on UI or API level. 要测试该结果是否正确显示,可以在UI或API级别进行测试。 If there is additional logic in the UI on how this is shown, that might be a test on UI level. 如果UI中还有其他显示方式,则可能是对UI级别的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM