代码之家  ›  专栏  ›  技术社区  ›  Ragini

在开始时两次调用前钩子和后钩子

  •  1
  • Ragini  · 技术社区  · 9 年前

    我对自动化测试和Cucumber也很陌生。我编写了一个简单的Cumber Junit示例,其中包含三个场景。

    @Before 在每个场景开始时调用,并且 @After 在每个场景后调用。因此,总的来说,它们应该只执行三次,因为有三种场景。

    当我运行以下步骤定义时,首先 @之前 然后调用 ,然后再次 @之前 @之后 @之前 @之后 ,然后直接使用场景3。因此没有 和 @之后 分别在场景3之前和之后调用。另一方面,在第一个场景执行之前,它们会被调用两次。知道我做错了什么吗?我的钩子项目中没有任何.rb文件。

    @Before contains : System.out.println("Setup performed"); @之后 System.out.println("CleanUp Performed");

    import cucumber.api.PendingException;
    import cucumber.api.java.After;
    import cucumber.api.java.Before;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    
    public class CalculatorSteps {
        private Calculator calculator;
    
        @Before
        public void setUp() {
            calculator = new Calculator();
            System.out.println("Setup Performed...");
        }
    
        //Scenario : add two numbers - With regular expression
        @Given("^I have a calculator$")
        public void i_have_a_calculator() {
            assertNotNull(calculator);
        }
    
        @When("^I add (\\d+) and (\\d+)$")
        public void i_add(int arg1, int arg2) {
            calculator.add(arg1, arg2);
        }
    
        @Then("^the result should be (\\d+)$")
        public void the_result_should_be(int result) {
            assertEquals(result, calculator.getResult());
        }
    
        //Scenario : Subtract one number from another - With regular expression
    
        @Given("^I have a calculatorr$")
        public void i_have_a_calculator1() throws Throwable {
            assertNotNull(calculator);
        }
    
        @When("^I subtract (\\d+.\\d+) from (\\d+.\\d+)$")
        public void i_subtract_from(int arg1, int arg2) {
        calculator.subtract(arg1, arg2);
        }
    
        @Then("^the result should be (\\d+.\\d+)$")
        public void the_result_should_be1(double result1) {
            assertEquals(result1, calculator.getresult1(), 0.5);
        } 
    
        @After
        public void cleanUp() {
            System.out.println("CleanUp Performed...");
        }
    }
    

    功能文件如下所示:

        Feature: Calculator
          I use Calculator instead of calculating myself
    
          #Scenario: Add two numbers
            #Given I have a calculator
            #When I add 2 and 3
            #Then the result should be 5
    
         @smokeTest
          Scenario Outline: Add two numbers
            Given I have a calculator
                When I add <num1> and <num2>
            Then the result should be <ans>
    
            Examples:
            | num1 | num2 | ans |
            | 2    | 3    | 5   |
            | 4    | 5    | 9   |
    
          @regressionTest
          Scenario: Subtract one number from another
            Given I have a calculator
            When I subtract 2.5 from 7.5
            Then the result should be 5.0
    

        Feature: Calculator
          I use Calculator instead of calculating myself
        Setup Performed...
        CleanUp Performed...
        Setup Performed...
    
          Scenario Outline: Add two numbers # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:19
            Given I have a calculator       # CalculatorSteps.i_have_a_calculator()
            When I add 2 and 3              # CalculatorSteps.i_add(int,int)
            Then the result should be 5     # CalculatorSteps.the_result_should_be(int)
        CleanUp Performed...
        Setup Performed...
    
          Scenario Outline: Add two numbers # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:20
            Given I have a calculator       # CalculatorSteps.i_have_a_calculator()
            When I add 4 and 5              # CalculatorSteps.i_add(int,int)
            Then the result should be 9     # CalculatorSteps.the_result_should_be(int)
        CleanUp Performed...
    
          #@regressionTest
          Scenario: Subtract one number from another # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:23
            Given I have a calculator                # CalculatorSteps.i_have_a_calculator()
            When I subtract 2.5 from 7.5             # CalculatorSteps.i_subtract_from(int,int)
            Then the result should be 5.0            # CalculatorSteps.the_result_should_be1(double)
    
        3 Scenarios (3 passed)
        9 Steps (9 passed)
        0m0,074s
    

    试过之后 counter 和@DaveyDaveDave代码,输出如下:开始时计数器行的打印位置不正确,但至少要正确执行SEM。但我仍然想知道为什么它不会执行 , @Given 和 在最后一个减法场景中。

    Feature: Calculator
      I use Calculator instead of calculating myself
    @Before Count is: 0
    @Given Count is: 1
    @After Count is: 2
    @Before Count is: 3
    
      @smokeTest
      Scenario Outline: Add two numbers # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:19
        Given I have a calculator       # CalculatorSteps.i_have_a_calculator()
        When I add 2 and 3              # CalculatorSteps.i_add(int,int)
        Then the result should be 5     # CalculatorSteps.the_result_should_be(int)
    @Given Count is: 4
    @After Count is: 5
    @Before Count is: 6
    
      @smokeTest
      Scenario Outline: Add two numbers # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:20
        Given I have a calculator       # CalculatorSteps.i_have_a_calculator()
        When I add 4 and 5              # CalculatorSteps.i_add(int,int)
        Then the result should be 9     # CalculatorSteps.the_result_should_be(int)
    @After Count is: 7
    
      @regressionTest
      Scenario: Subtract one number from another # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:23
        #Given I have a calculator
        When I subtract 2.5 from 7.5             # CalculatorSteps.i_subtract_from(int,int)
        Then the result should be 5.0            # CalculatorSteps.the_result_should_be1(double)
    
    3 Scenarios (3 passed)
    8 Steps (8 passed)
    0m0,072s
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   DaveyDaveDave max    9 年前

    我认为功能 是 按照您期望的顺序执行,这只是一个如何以及何时显示输出的问题。

    我刚刚复制并粘贴了您的源代码,并添加了一个 counter 变量,当我运行它时,我会看到以下输出:

    Setup Performed... 0
    In given...1
    ...CleanUp Performed... 2
    Setup Performed... 3
    In given...4
    ...CleanUp Performed... 5
    Setup Performed... 6
    In given...7
    ...CleanUp Performed... 8
    

    RequiresSetupCalculator 如果在执行操作之前没有设置,并且如果我们在没有调用之前尝试运行设置,则会引发异常 cleanUp .

    public class RequiresSetupCalculator {
      private static RequiresSetupCalculator instance;
    
      private int result = 0;
      private double result1 = 0;
    
      private boolean isSetup;
    
      private RequiresSetupCalculator() {
      }
    
      public static synchronized RequiresSetupCalculator setup() {
        if (instance == null) {
            instance = new RequiresSetupCalculator();
        }
    
        if (instance.isSetup) {
            throw new RuntimeException("I have already been set up");
        }
    
        instance.isSetup = true;
    
        return instance;
      }
    
      public void cleanUp() {
        if (!isSetup) {
            throw new RuntimeException("I wasn't set up in the first place");
        }
    
        isSetup = false;
      }
    
      public void add(final int arg1,
                    final int arg2) {
        if (!isSetup) {
            throw new RuntimeException("I haven't been set up, no calculating for you!");
        }
    
        result = arg1 + arg2;
      }
    
      public int getResult() {
        if (!isSetup) {
            throw new RuntimeException("I haven't been set up, no calculating for you!");
        }
    
        return result;
      }
    
      public void subtract(final int arg1,
                         final int arg2) {
        if (!isSetup) {
            throw new RuntimeException("I haven't been set up, no calculating for you!");
        }
    
        result1 = arg2 - arg1;
      }
    
      public double getresult1() {
        if (!isSetup) {
            throw new RuntimeException("I haven't been set up, no calculating for you!");
        }
    
        return result1;
      }
    }
    

    Calculator ,然后修改 CalculatorSteps 使用 并致电 setup 和 清理 @Before @After 您的测试方法如下:

    public class CalculatorSteps {
      private RequiresSetupCalculator calculator;
    
      @Before
      public void setUp() {
        calculator = RequiresSetupCalculator.setup();
        System.out.println("Setup Performed...");
      }
    
      ... (existing test steps unchanged)...
    
      @After
      public void cleanUp() {
        System.out.println("CleanUp Performed...");
        calculator.cleanUp();
      }
    }
    

    @之前 @之后 方法在每次测试后运行,也只有一次。

    对我来说,这是成功的。如果在运行测试时抛出异常,那么会发生一些奇怪的事情,但我很有信心您会得到与我相同的结果。

    推荐文章