如何处理BDD中的环境先决条件?

时间:2019-06-11 12:39:36

标签: pytest bdd

我正在从事一个自动化测试项目(使用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

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

2 个答案:

答案 0 :(得分:2)

一种方法-使用@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);
    }
}

其他方式-使用后台(在每种情况下进行设置)

黄瓜为此提供了一种机制,方法是提供一个 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();
    }

答案 1 :(得分:1)

使用黄瓜的“背景” keyword

  

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

另请参阅官方docs