很难测试,也不会忘记任何事情,因此,我为我的数据库的所有功能编写了Cucumber场景(插入,选择等,请求函数,程序等,以及视图)
当我们测试所有这些的行为时,这对我们有很大帮助,甚至在编写视图和其他代码之前,确定我们真正想做的事情是非常有帮助的.
我的问题是:在编写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$$
黄瓜特色:
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个结果,则不会立即知道这两个值中的哪一个不正确,而您不知道您获得的值是什么;获取该信息需要更多调查.
通过将对这两个值的检查分成两个RSpec测试,当测试运行完毕后,您可以知道两者是否正确,一个是否正确,或两者都不正确.如果其中一个或两个都不正确,RSpec也会为失败的测试返回一条消息“预期3,得到4”,这可以帮助您更快地进行调试.
当您为不同的输入添加更多测试时,我建议重构我在此处给出的伪代码以使用shared_examples_for.您正在阅读的PragProg RSpec书籍是一个很好的参考.
