如何基于SQL Server查询结果通过/失败黄瓜测试步骤

时间:2018-06-06 08:54:43

标签: java automated-tests cucumber

我正在使用Cucumber / Gherkin语法编写自动化测试用例。

一个特定的方案涉及验证是否已成功将行添加到数据库中。

所以我希望测试基于SELECT语句传递/失败,该语句检查特定ID是否已持久保存到数据库。

如果传入SELECT语句的ID确实存在,则PASS。

如果数据库中不存在该ID,则为FAIL。

目前,我能够显示行数据(因此ID被保留),但即使我查询了无效的ID,测试仍然会通过。

以下是我目前的代码:

 @Then("BR API will insert a record into tBookingRequestRejections table in the BR Staging DB tables. The record will contain the error code and error description.")

 public void br_API_will_insert_a_record_into_tBookingRequestRejections_table_in_the_BR_Staging_DB_tables_The_record_will_contain_the_error_code_and_error_description() {

    try {

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection m_Connection = DriverManager.getConnection(DETAILS);
        Statement m_Statement = m_Connection.createStatement();         
        String query = "select top 10 * from tBookingRequest where request_id = " + idNumber; 

        ResultSet m_ResultSet = m_Statement.executeQuery(query);

        while (m_ResultSet.next()) {
            System.out.println("HERE: " + m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", "
                    + m_ResultSet.getString(3));
        }
    }
    catch(Exception ex) {
        System.out.println(ex.toString());
    }

如何根据此查询返回的值使Cucumber Test通过/失败?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

一般来说,在进行限制时,您应该通过用户界面进行验证,而不是直接查看数据库。如果您的用户无法看到已添加的记录,或者您的api未确认该记录已添加,则该行为将不可见。

现在这并不意味着你不应该在数据库中测试记录,它建议你可以写一个更快的单元测试来做这件事,或者你应该改进交互所以你可以测试可见的结果。

值得记住的是,写得好的单元测试可以比Cucumber场景快一个,两个甚至三个数量级(取决于你测试的内容)。