21

测试数据管理策略

 3 years ago
source link: https://www.qaseven.cn/posts/测试数据管理策略.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#测试数据管理策略

你好 所有的读者,

今天我将讨论在运行自动化测试时处理测试数据的一些不同方法和权衡。

在使用SQL、mysql或postgresql脚本运行测试之前注入数据是最常见的方法之一。因此,您可以注入测试所需的数据并跳过所有设置,这不是所有场景的目标,对吗?

对于实际需要测试数据创建的场景,则不需要使用这种脚本。例如,在javascript中,你可以添加一个设置/数据管理类,一个@BeforeAll,然后像这样:

var mysql = require('mysql');
var con = mysql.createConnection({
     host: "localhost",
     user: "root",
     password: "12345",
     database: "javatpoint"
});  

con.connect(function(err) {
     if (err) throw err;
       console.log("Connected!");
       var sql = "INSERT INTO employees (id, name, age, city) VALUES ('1', 'Ajeet Kumar', '27', 'Allahabad')"; 
       con.query(sql, function (err, result) {
     if (err) throw err;
       console.log("1 record inserted");  
     });
});

然后您可以使用@TearDown和@AfterAll函数来删除在测试期间使用的创建的数据。

例如,如果您正在运行一些API测试,您可能希望为每个场景注入静态数据。您可以创建一个json文件,并添加将在自动化过程中使用的所有字段和值

{ 
   name: "John", 
   age: 31, 
   city: "New York" 
},
{
   name: "Rafa", 
   age: 29, 
   city: "London" 
}

然后您可以加载此文件以在测试期间使用。您可以预先创建此数据,但随后您需要确保该数据始终存在,否则您需要再次创建(在测试期间或手动创建)。

您可以使用自动测试所需的数据来创建对象,例如,您可以使用Javascript创建字典:

var dict = {
  FirstName: "Rafa",
  Age: 30,
  Country: "UK"
};

然后再次需要确保将在运行时创建此数据,可能是在@BeforeAll函数或Setup类中,或者这可能是您已经在环境中创建的,并且需要确保将 运行测试时请在那里,否则您需要重新创建它。

如果您可以控制数据库或QA环境的部署,那么这意味着您还可以在运行测试时操纵数据库。

如果使用docker创建环境,则可以添加卷,甚至可以使用docker-compose为数据库添加seed。

与将数据持久保存在容器的可写层中相比,卷通常是更好的选择,因为卷不会增加使用卷的容器的大小,并且卷的内容存在于给定容器的生命周期之外。

您可以将数据库(json文件,.db)完全推送到docker容器:

docker run -it --name my-directory-test -v /hostvolume:/containervolume centos /bin/bash

编写一个小的脚本,该脚本生成随机的和变化的数据并将其写入数据库。 然后,您可以将此脚本包装到自己的Docker映像中,以便通过docker-compose自动执行它们。

在此示例中,我使用的是mongoDB数据库: docker-compose.yml

version: '1.0'

services:

  mongodb:
    image: mongo
    container_name: mongo
    ports:
      - 27017:27017


  mongo-seed:
    build: .
    environment:
      - MONGODB_HOST=mongo
      - MONGODB_PORT=27017
    volumes:
      - ./config/db-seed:/data
    depends_on:
      - mongo
    command: [
      "mongoimport --host mongo --port 27017 --db testautomation --mode upsert --type json --file data.json --jsonArray"
      ]

data.json

[
  {
    "name": "Peter Parker",
    "email": "[email protected]",
    "age": 28
  },
  {
    "name": "Bruce Wayne",
    "email": "[email protected]",
    "age": 48
   }
]

如果您使用的是Gherkin语法,则意味着您也可以在场景中间添加数据,然后在自动化过程中使用它们。 因此,类似:

Scenario: Correct number of movies found by superhero
Given I have the following movies
| Batman Begins | Batman |
| Wonder Woman | Wonder Woman |
| Wonder Woman 1984 | Wonder Woman |
When I search for movies by superhero Wonder Woman
Then I find 2 movies

然后,您可以从步骤定义中获取此数据,并在测试中使用。

您可能还有其他方式来创建和管理测试数据,但是无论您决定采用哪种方法,都要确保方案是独立的,以及之后是否可以清理环境数据(除非您现在暂时决定在环境中保存静态数据) ),然后将其清洁。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK