首页 > 解决方案 > 使用 Jest 进行数据驱动测试

问题描述

需要使用从数据库中检索的值来自动化测试。此外,需要使用一组数据运行相同的测试。

我使用 Jest 测试框架并且必须使用 it.test() 来测试数据。问题是测试文件首先检查我传递给 it.test() 的数组,最初它是空的。请检查随附的代码。

import "jest";

describe('Document Training test suit', function(this:any){

let docTrainingPage: DocumentTraining;   
let driver: any;    
let valuesArray:Array<any>=[];    
var i=0;

beforeAll(async()=>{       
    const driver = await new ConfigBrowser();
    this.driver = driver;
    docTrainingPage = new DocumentTraining(this.driver);
    await docTrainingPage.connectToDB();      

    valuesArray = await docTrainingPage.retrieveValues();//Retrieve value from database
});   


describe('Should not save the document',  async() => { 

    it.each(valuesArray )('Should NOT savedocuments with INVALID values',async() => {       
        const A= JSON.stringify(valuesArray[i].AAA);
        const B= JSON.stringify(valuesArray[i].BBB);
        const C= JSON.stringify(valuesArray[i].CCC);
        const D= JSON.stringify(valuesArray[i].DDD);
        const E= JSON.stringify(valuesArray[i].EEE);           

        await docTrainingPage.enterValues(A, B, C, D, E);

        const testResult = await docTrainingPage.verifyValidDocTrainingSuccessful();
        expect(testResult).toBeTruthy();

        i++;
    });        
})   

afterAll(()=>{
    docTrainingPage.endConnection();
    setTimeout(() => process.exit(), 1000)       
});

})

我尝试调试此代码。它在开始时首先到达beforeAll但随后它会去检查是否有任何有效的测试。此时,数组未加载。

错误

● 测试套件无法运行

Your test suite must contain at least one test.

在此处输入图像描述

标签: mysqlseleniumautomated-testsjestjsdata-driven-tests

解决方案


推荐阅读