【问题标题】:How to handle environment prerequisites in BDD?如何处理 BDD 中的环境先决条件?
【发布时间】:2020-10-13 16:06:20
【问题描述】:

我正在从事一个自动化测试项目(使用 Pytest BDD),我经常遇到如何使用 BDD 和 Gherkin 处理环境先决条件的问题。例如,几乎所有场景都需要创建新实体(用户/管理员/站点/组织/等),只是为了有一些东西可以使用。

我认为我不应该在“给定”部分中写下所有先决条件操作(这似乎是反 BDD),但我不想迷失在什么场景设置什么以及如何设置的问题上。

例如,我永远不想创建这个:

Scenario: A user can buy a ticket from a webshop.
Given an item available in the webshop
And a user is created
And the user has at least one payment option set up
And the user is logged in
When the user buys the item in the webshop
Then the user owns the item

人们通常如何以将来可读和可维护的方式写下这些动作和实体?

【问题讨论】:

    标签: python pytest bdd


    【解决方案1】:

    一种方式 - 使用 @BeforeClass(一次性设置)

    @RunWith(Cucumber.class)
    @CucumberOptions(features = "classpath:features/checkoutmodule/registereduser/",
                         glue = {"com.ann.automation.test.steps" },
                         tags = { "@SignIn" },
                       plugin = { "pretty","json:target/cucumber.json",
                                  "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports",
                                  "com.cucumber.listener.ExtentCucumberFormatter"},
                       strict = false,
                       dryRun = false,
                   monochrome = true)
    
    public class RunCuke {
    
        // ----------------------------- Extent Report Configuration -----------------------------
        @BeforeClass
        public static void setup() {
            // below is dummy code just to showcase
            File newFile = new File(Constants.EXTENT_REPORT_PATH);
            ExtentCucumberFormatter.initiateExtentCucumberFormatter(newFile,true);
            ExtentCucumberFormatter.loadConfig(new File(Constants.EXTENT_CONFIG_FILE_PATH));
            ExtentCucumberFormatter.addSystemInfo("Browser Name", Constants.BROWSER);
            ExtentCucumberFormatter.addSystemInfo("Browser version", Constants.BROWSER_VERSION);
            ExtentCucumberFormatter.addSystemInfo("Selenium version", Constants.SELENIUM_VERSION);
        }
    }
    

    其他方式 - 使用背景(在每个场景之前设置)

    Cucumber 通过提供 Background 关键字为此提供了一种机制 你可以在哪里指定

    • 功能文件中所有测试通用的一个或一系列步骤。
    • 应该在每个场景之前运行一个或一系列步骤 特征。通常这些将是给定的步骤,但您可以使用任何步骤 你需要的。

    示例:在这里,在每个场景/大纲执行之前,我们希望用户进入网站的主页并搜索产品。那么让我们看看实现。

      Background: 
        Given User is on Brand Home Page "https://www.anntaylor.com/"
        Given User searches for a styleId for <Site> and makes product selection on the basis of given color and size
          | Style_ID  | Product_Size | Product_Color |
          | TestData1 | TestData1    | TestData1     |
          | TestData2 | TestData2    | TestData2     |
    
      @guest_search
      Scenario Outline: Validation of UseCase Guest User Order Placement flow from Search
        Then Clicking on Cart icon shall take user to Shopping Bag
        When Proceeding to checkout as "GuestUser" with emailId <EmailID> shall take user to Shipping Page
        And Entering FN as <FName> LN as <LName> Add as <AddL1> ZCode as <ZipCode> PNo as <PhoneNo> shall take user to payment page
        And Submitting CCardNo as <CCNo>" Month as <CCMonth> Year as <CCYear> and CVV as <CVV> shall take user to Order Review Page
        Then Verify Order gets placed successfully
    
        Examples: Checkout User Information
          | EmailID   | FName     | LName     | AddL1     | ZipCode   | PhoneNo   | CCNo      | CCMonth   | CCYear    | CVV       |
          | TestData2 | TestData2 | TestData2 | TestData2 | TestData2 | TestData2 | TestData2 | TestData2 | TestData2 | TestData2 |
    

    最后的方法 - 使用 @Before(在每个场景之前设置)

    @Before
        public void setUpScenario(Scenario scenario){
            log.info("***** FEATURE FILE :-- " + Utility.featureFileName(scenario.getId().split(";")[0].replace("-"," ")) + " --: *****");
            log.info("---------- Scenario Name :-- " + scenario.getName() + "----------");
            log.info("---------- Scenario Execution Started at " + Utility.getCurrentTime() + "----------");
            BasePage.message=scenario;
            ExtentTestManager.startTest("Scenario No . " + (x = x + 1) + " : " + scenario.getName());
            ExtentTestManager.getTest().log(Status.INFO, "Scenario No . "+ x + " Started : - " + scenario.getName());
        //  Utility.setupAUTTestRecorder();
            // --------- Opening Browser() before every test case execution for the URL given in Feature File. ---------
            BaseSteps.getInstance().getBrowserInstantiation();
        }
    

    【讨论】:

      【解决方案2】:

      使用 Cucumber 的“背景”keyword:

      Cucumber 中的背景用于定义特征文件中所有测试通用的一个或一系列步骤。它允许您为定义它的功能的场景添加一些上下文。

      另见官方docs

      【讨论】:

      • 我知道“背景”,但它不能解决我的问题。我需要从场景到场景的不同设置步骤。 “背景”中的步骤在每个场景之后执行,所以我只能准备一种设置。另外:我仍然需要在 BDD 文件中写下我想避免的所有必要步骤,以保持功能文件的可读性。
      • 然后您可以使用 @Before 钩子,该钩子仅在使用您选择的标签标记的场景之前执行。然后你可以把任何你需要的东西放在钩子里。
      • hm,@Before 钩子也无法解决问题。它需要在每个功能文件中复制代码(标签)。作为解决方案之一 -> 使用具有一组用户/头脑/组织/等的固定装置。
      猜你喜欢
      • 2020-02-05
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多