mysql - 为 MySQL 自动化 Cucumber 测试场景

标签 mysql tdd rspec cucumber bdd

我已经构建了一个重要的 MySQL 数据库,其中包含大量 View 、触发器、函数和过程。

很难测试,也很难忘记任何东西,因此,我为我的数据库的所有功能编写了 Cucumber 场景(插入、选择等,对函数、过程等的请求,以及 View )

当我们测试所有这些的行为时,这对我们有很大帮助,甚至在编写 View 和其他代码之前,确定我们真正想要做的事情非常有帮助。

我的问题是:在编写 Cucumber 功能后,我们都在 MySQL Shell 中手动测试。

我是 BDD/TDD 和敏捷方法方面的新手,但我进行了一些搜索以了解如何进行一些自动化,但没有发现任何对我的案例非常有趣的东西。

有没有人可以提供一些有趣的方法来为此创建自动化?

我不懂 Ruby,但举个例子,是否可以将 RSPec 直接与 MySQL 一起使用(有一些例子)?

或者用另一种语言,或者你能想到的任何解决方案!

提前致谢!

[编辑]


如果用 RSpec 和 MySQL 发现了一些有趣的东西:

Mysql Support For Cucumber Nagios

mysql_steps.rb


我的问题是:我对 Ruby、RSPec 等一无所知。

我正在使用优秀的“Pick Axe”书和 PragProg 的 RSPec 书来研究它

但是我将非常感谢给定以下代码的 RSpec 步骤的一个小例子:


MySQL 过程

DELIMITER $$

CREATE PROCEDURE `prc_liste_motif` (
    IN texte TEXT,
    IN motif VARCHAR(255),
    OUT nb_motif INT(9),
    OUT positions TEXT)
BEGIN
    DECLARE ER_SYNTAXE CONDITION FOR SQLSTATE '45000';
    DECLARE sousChaine TEXT;
    DECLARE positionActuelle INT(9) DEFAULT 1;
    DECLARE i INT(9) DEFAULT 1;

    IF
        LENGTH(motif) > LENGTH(texte)
    THEN
        SIGNAL ER_SYNTAXE
            SET MESSAGE_TEXT =
              'Bad Request: Le motif est plus long que le texte.',
              MYSQL_ERRNO = 400;
    END IF;

    SET positions = '';
    SET nb_motif = 0;

    REPEAT

        SET sousChaine = SUBSTRING_INDEX(texte, motif, i);

        SET positionActuelle = LENGTH(sousChaine) + 1;

        IF
          positionActuelle < LENGTH(texte) + 1
        THEN

            IF
              LENGTH(positions) > 0
            THEN
                SET positions = CONCAT(positions, ',');
            END IF;

            SET positions = CONCAT(positions, positionActuelle);

            SET nb_motif = nb_motif + 1;

        END IF;

        SET i = i + 1;

    UNTIL LENGTH(sousChaine) >= LENGTH(texte)
    END REPEAT;

END$$

cucumber 功能:

Feature: Procedure prc_liste_motif
  In order to precess a string according to a given unit
  I want to know the number of units present in the chain and their positions
  Knowing that the index starts at 1

  Background: the database mydatabase in our SGBDR server
    Given I have a MySQL server on 192.168.0.200
    And I use the username root
    And I use the password xfe356
    And I use the database mydatabase

  Scenario Outline: Using the procedure with good values in parameters
    Given I have a procedure prc_liste_motif
    And I have entered <texte> for the first parameter
    And I have entered <motif> for the second parameter
    And I have entered <nb_motif> for the third parameter
    And I have entered <positions> for the fourth parameter
    When I call prc_liste_motif
    Then I should have <out_nb_motif> instead of <nb_motif>
    Then I should have <out_positions> instead of <positions>

    Exemples:
      | texte         | motif | nb_motif | positions | out_nb_motif | out_positions |
      | Le beau chien | e     |          |           | 3            | 2,5,12        |
      | Allo          | ll    |          |           | 1            | 2             |
      | Allo          | w     |          |           | 0            |               |

在MySQL中手动通过测试的例子:

$ mysql -h 192.168.0.200 -u root -p xfe356
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> USE mydatabase
Database changed
mysql> SET @texte = 'Le beau chien';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @motif = 'e';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @nb_motif = NULL;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @positions = NULL;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @out_nb_motif = 3;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @out_positions = '2,5,12';
Query OK, 0 rows affected (0.00 sec)

mysql> CALL prc_liste_motif(@texte, @motif, @nb_motif, @positions);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @nb_motif = @out_nb_motif AND @positions = @out_positions;
+-----------------------------------------------------------+
| @nb_motif = @out_nb_motif AND @positions = @out_positions |
+-----------------------------------------------------------+
|                                                         1 |
+-----------------------------------------------------------+
1 row in set (0.00 sec)

预先感谢您的帮助!

最佳答案

这里是一些伪代码,用于使用 RSpec 测试数据库的一种方法:

describe "prc_liste_motif" do
  before(:all) do
    # Set up database connection here
  end

  describe "good values" do
    context "Le beau chien" do
      let(:texte) { "Le beau chien" }
      # Set up other variables here
      let(:results) { # call prc_liste_motif here }

      it "has the correct out_nb_motif" do
        out_nb_motif = # however you derive this from the results of the procedure
        out_nb_motif.should == 3
      end

      it "has the correct out_positions" do
        # test out_positions here
      end
    end
  end
end

我在您的示例手动测试中注意到的一件事是您检查结果的方式:

 SELECT @nb_motif = @out_nb_motif AND @positions = @out_positions;

这会告诉您这两个值是否正确,但是如果您对此查询得到 0 个结果,您不会立即知道这两个值中的哪个不正确,您也不知道您得到的值是什么是;获取该信息需要更多调查。

通过将对这两个值的检查拆分为 2 个 RSpec 测试,当测试完成运行时,您可以知道两者是否正确,一个不正确,或者两个都不正确。如果其中一个或两个不正确,RSpec 还会针对失败的测试返回一条消息,显示“Expected 3, got 4”,这可以帮助您更快地进行调试。

当您为不同的输入添加更多测试时,我建议重构我在此处给出的伪代码以使用 shared_examples_for。您正在阅读的 PragProg RSpec 一书是很好的引用。

关于mysql - 为 MySQL 自动化 Cucumber 测试场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5205240/

相关文章:

php - 在 PHP 页面上列出 WordPress 网站的所有帖子

php - Google map 文档使用已弃用的 PHP - 我该如何更新它?

Mysql JSON 列任何索引([*])不工作

java - Glassfish4 和 MySQL 5.5.38 远程服务器 : com. mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

java - 如何在 Spring Boot 应用程序中测试 native 查询?

java - mockito 是否应该调用模拟类的默认构造函数?

jquery - 如何测试 javascript 交互,例如在 html5 canvas 中用鼠标绘图

ruby - Watir/Rspec 浏览器循环

ruby-on-rails - rspec - 为什么 be_valid 不起作用

ruby - Rspec:应该是(这个或那个)