【问题标题】:Run each cucumber test independently独立运行每个黄瓜测试
【发布时间】:2017-10-28 15:27:36
【问题描述】:

我在 Eclipse 上使用 cucumber 和 maven,我想要做的是独立运行每个测试。例如,我有一个图书馆系统软件,它基本上允许用户借书和做其他事情。

其中一个条件是用户最多只能借两本书,所以我写信是为了确保该功能正常工作。这是我的功能文件:

Scenario: Borrow over max limit
Given "jim@help.ca" logs in to the library system
When "jim@help.ca" order his first book with ISBN "9781611687910"
And "jim@help.ca" orders another book with ISBN "9781442667181"
And "jim@help.ca" tries to order another book with ISBN "1234567890123"
Then jim will get the message that says "The User has reached his/her max number of books"

我写了一个相应的步骤定义文件,每一个都很好。但是,将来我想使用相同的用户名(“jim@help.ca”)来借书,就好像 jim@help.ca 还没有借过任何书一样。我希望每个测试彼此独立。

有什么方法可以做到这一点...也许我可以将一些东西放入我的步骤定义类中,例如拆卸方法。我已经调查过了,但我无法找到任何关于它的可靠信息。如果有办法请帮助我。任何帮助都非常感谢,我提前感谢您!

【问题讨论】:

  • “以后我想用同一个用户名 (jim@help.ca) 来借书,就好像 Sun@help.ca 还没有借过书一样”是什么意思?
  • 抱歉,改成 jim@help.ca

标签: java maven testing cucumber


【解决方案1】:

是的,您可以在每个场景之前和之后进行设置和拆卸,但它不在步骤定义文件中。您要使用的是钩子。

Hooks 在场景之前或之后运行,并且可以在每个场景之前/之后运行,或者只是你和 @tag 到的那些,例如:

@remove_borrowed_books
Scenario: Borrow over max limit

不幸的是,我只用过 cucumber 和 ruby​​ 而不是 java,所以我不能给你一步一步的指导,但这应该告诉你你需要知道什么https://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

【讨论】:

    【解决方案2】:

    您可以使用“@After”钩子来实现这一点,正如@Derek 提到的那样,例如使用每个用户名借阅的图书地图:

    private final Map<String, Integer> booksBorrowed = new HashMap<>();
    
    @After
    public void tearDown() {
      booksBorrowed.clear();
    }
    
    @Given("...")
    public void givenUserBorrowsBook(String username) {
      booksBorrowed.put(username, booksBorrowed.containsKey(username) ? booksBorrowed.get(username) + 1 : 1);
      ....
    }
    

    或者“@Before”钩子在每个场景执行之前执行清理,这是我推荐的选项:

    private Map<String, Integer> booksBorrowed;
    
    @Before
    public void setUp() {
      booksBorrowed = new HashMap<>();
    }
    

    如果您计划并行运行场景,那么逻辑会更复杂,因为您需要维护执行特定场景的线程与该线程上使用的用户名之间的关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2020-12-19
      • 2020-03-28
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多