天天看點

#測試架構推薦# test4j,資料庫測試# 背景# 介紹# 重點# 特點

# 背景

後端都是操作DB的,這塊的自動化測試校驗的話,是需要資料庫操作的,當然可以直接封裝方法來操作資料,那麼有沒有開源架構支援資料操作,讓我們關注寫sql語句?或者幫我們做mysql的斷言呢?

# 介紹

test4j,github位址:https://github.com/test4j,看名字感覺還是公司的大神維護了一段時間的,膜拜大神,Orz

Test4J原名叫jTester,本來是釋出在google上的一個開源項目,後來遷移到github,并且由于域名的緣故,更名為[Test4J]

Test4J是一個單元測試和業務流程測試架構,其基本功能包括如下:

https://github.com/test4j/test4j#%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E5%8A%9F%E8%83%BD 單元測試功能

  • Fluent方式的斷言,内置了大部分常用的斷言文法,特别是對象反射斷言功能尤其強大。
  • Junit和testNg文法擴充,使用@DataFrom方式擴充junit的資料驅動測試功能;@Group文法讓junit支援分組測試;子產品嵌入的方式讓junit和testng支援功能擴充。
  • 內建jMockit架構,讓mock更自由自在。
  • 對象自動填充功能,反射工具。

https://github.com/test4j/test4j#%E9%9B%86%E6%88%90%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7%E5%8C%85 內建測試工具包

  • 支援Spring內建測試,spring容器可以mock對象,自定義對象無縫內建。
  • 資料庫測試支援,使用DataMap對象,Json資料準備資料,或者驗證資料,同時支援資料庫資料的Fluent斷言。

https://github.com/test4j/test4j#%E4%B8%9A%E5%8A%A1%E9%A9%B1%E5%8A%A8%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7%E5%8C%85 業務驅動測試工具包

  • 支援編寫可讀的用例,并在用例中嵌入測試用資料,架構自動轉換為可執行代碼。
  • 支援用例步驟的重複利用,簡化用例編寫難度。

# 重點

重點講的是資料庫測試這塊

1. 引入maven依賴

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring</artifactId>

  <version>2.5.6.SEC02</version>

</dependency>

  <groupId>commons-dbcp</groupId>

  <artifactId>commons-dbcp</artifactId>

  <version>1.4</version>

  <groupId>org.test4j</groupId>

  <artifactId>test4j.testng</artifactId>

  <version>2.0.6</version>

  <groupId>org.test4j</groupId>

  <artifactId>test4j.integrated</artifactId>

  <version>2.0.6</version>

2. 配置test4j.properties檔案

在resources目錄新增test4j.properties檔案

database.type=mysql
database.url=jdbc:mysql://localhost/presentationtdd?characterEncoding=UTF8
database.userName=root
database.password=password
database.schemaNames=presentationtdd
database.driverClassName=com.mysql.jdbc.Driver      

3 . demo代碼

package com.jwen.test;

import org.test4j.testng.Test4J;
import org.testng.annotations.Test;

import java.util.Date;

public class DataMapDemo extends Test4J {


    @Test
    public void datamaptest() {

        db.table("demo").clean().insert(1, new DataMap() {
            {
                this.put("name", "jwen");
            }
        }).commit();
    }

    @Test
    public void testEqMap() {
        db.table("demo").query().propertyEqMap(new DataMap() {
            {
                this.put("id", 4);
                this.put("name", "jwen1");
            }
        });
    }

    @Test
    public void insertMutiDate() {
        db.table("demo").clean().insert(5, new DataMap() {
            {
                this.put("id", DataGenerator.increase(100, 1));
                this.put("name", DataGenerator.random(String.class));
                this.put("email", new DataGenerator() {
                    @Override
                    public Object generate(int i) {
                        return value("name") + "@163.com";
                    }
                });
                this.put("day", new Object[]{new Date(), "2018-03-03"});
            }
        }).commit();
    }


    //{"id":1,"name":"jwen","email":"[email protected]","day":"2018-8-08"}
    @Test
    public void insertDataByJsonString() {
        db.table("demo").clean().insert("{\"id\":1,\"name\":\"jwen\",\"email\":\"[email protected]\",\"day\":\"2018-8-08\"}").commit();
    }

    @Test
    public void testExecute(){

    }
}      

 當然這都是一些基本操作

# 特點

1. 使用過java的mybatis,python的SQLAlchemy,目前test4j的資料庫這塊更加簡潔易懂

2. 支援斷言,這個是很難得,我之前的測試思路把查詢出來的結果變成json串,然後通過JsonAssert去斷言;

3. 斷言結果友好,可以提示到哪個字段錯誤;

雖千萬人,吾往矣!