Unit testing 带有DbUnit测试用例慢度的Spock

Unit testing 带有DbUnit测试用例慢度的Spock,unit-testing,spock,dbunit,Unit Testing,Spock,Dbunit,作为自动化单元测试的一部分,我们已经实现了Spock+Db unit框架 我们现在有2000个测试用例(特性),用于150种规格的DbUnit 在这里,我们在数据库中添加必需的条目,然后测试每个方法的行为 我们观察到,执行这些测试用例大约需要2小时30分钟 我在一个特性方法中添加了时间戳setupfixture。以下是我的观察: allergy.dao.AllergyFormDAOSpec>获取过敏表标准 安装方法执行开始于:2018年1月12日星期五19:00:42 allergy.dao.A

作为自动化单元测试的一部分,我们已经实现了Spock+Db unit框架

我们现在有2000个测试用例(特性),用于150种规格的DbUnit

在这里,我们在数据库中添加必需的条目,然后测试每个方法的行为

我们观察到,执行这些测试用例大约需要2小时30分钟

我在一个特性方法中添加了时间戳
setup
fixture。以下是我的观察:

allergy.dao.AllergyFormDAOSpec>获取过敏表标准
安装方法执行开始于:2018年1月12日星期五19:00:42
allergy.dao.AllergyFormDAOSpec>API以获取标准过敏表单
特征方法执行开始于:2018年1月12日星期五19:00:44
特征方法执行于2018年1月12日星期五19:00:45结束
运行一个测试用例所用的总时间:242
清理方法执行开始于:2018年1月12日星期五19:00:45
运行要素方法所用的总时间:2531
在这里,我观察到在安装之后加载一个特性方法平均需要2-4秒。但是,原始测试用例的执行时间不到一秒钟

我想知道我是否能得到关于这里可能延迟什么的建议?如前所述,对于2000个测试用例,3秒意味着Spock所花费的时间几乎是1小时30分钟,而不是真正的功能执行

总之,我们希望在每天运行Spock测试用例时减少它所花费的总时间

规格

package.dao
导入java.util.Date
导入org.dbunit.IDatabaseTester;
导入org.dbunit.ext.mssql.insertidityOperation;
进口变态反应;
导入be.janbols.spock.extension.dbunit.dbunit;
导入spock.lang.Shared
导入util.MasterSpec
类AllergyFormDAOSpec扩展了MasterSpec{
def dao=新的过敏原FORMDAO();
@共享日期时间起点1
@共享日期时间结束1
@DbUnit(配置={IDatabaseTester it->
it.setUpOperation=InsertIdentityOperation.REFRESH
it.tearDownOperation=InsertIdentityOperation.DELETE
})
def内容={
过敏表格(formId:99999,formName:'DummySpockForm',displayIndex:1,deleteFlag:0,is_biological:1)
过敏表格设施(id:99999,formId:99999,facilityid:2)
form_浓度(id:99999,formId:99999,名称:'1:100',deleteflag:0,displayindex:1)
}
def设置(){
timeStart1=新日期()
println“安装方法执行开始于:”+timeStart1
}
def“API测试删除表单设施”(){
def startTime=新日期()
println“功能方法执行开始于:“+startTime”
给定:“表单Id已给定”
def formId=99999
何时:“删除表单设施”
def result=dao.deleteFormFacilities(null,formId)
然后:“验证结果”
(结果>0)=真
def endTime=新日期()
println“功能方法执行在:“+endTime”结束
println“运行一个测试用例所用的总时间:”+(endTime.getTime()-startTime.getTime())
}
def cleanup(){
timeEnd1=新日期()
println“清理方法执行开始于:”+timeEnd1
def差异=TIMEND1.time-timeStart1.time
println“运行夹具方法所花费的总时间:”+差异
}
}
主规范

package-util
导入com.ecw.dao.SqlTranslator
导入目录.Root
导入spock.lang.Shared
导入spock.lang.Specification
导入javax.sql.DataSource
/**
*/
类MasterSpec扩展了规范{
@共享
属性=新属性()
@共享
公共数据源数据源
@共享
受保护的xmlDataSource=[:]
静态int timeCntr=0;
//安装程序是在xmlDataSource Hashmap中读取xml文件的内容
def设置(){
//获取不包含其包的正在运行的类名
def className=this.class.name.substring(this.class.name.lastIndexOf('.')+1)
def resourceAnno=specificationContext.currentFeature.featureMethod.getAnnotation(文件资源)
如果(resourceAnno!=null){
def files=resourceAnno.xmlFiles()
def packageName=(this.class.package.name).replaceAll('\\.','/'))
for(int i=0;i-1){
strDBName=strDBName.substring(0,strDBName.indexOf('?'))
}
字符串strServerName=getPropertyValue(“myApp.DBHost”);
if(strServerName.indexOf(':')>-1){
strServerName=strServerName.substring(0,strServerName.indexOf(':'))
}
字符串strUrl=getPropertyValue(“myApp.DBUrl”)
String strPort=strUrl.substring(strUrl.lastIndexOf(':')+1)
//对于MSSQL
System.setProperty(“myApp.SkipJndi”,“是”)
//dataSource=新的JtdsDataSource()
对象newObject=null;
if(SqlTranslator.isDbSqlServer()){
newObject=Class.forName(“net.sourceforge.jtds.jdbcx.JtdsDataSource”).newInstance()
}else if(SqlTranslator.isDbMySql()){
newObject=Class.forName(“com.mysql.jdbc.jdbc2.optional.MysqlDataSource”).newInstance()
}
数据源=(数据源)新对象
dataSource.setDatabaseName(strDBName)
setUser(getPropertyValue(“myApp.DBUser”))
dataSource.setPassword(getPropertyValue(“myApp.DBPassword”))
dataSource.setServerName(strServerName)
dataSource.setPortNumber(Integer.parseInt(strPort))
}
}
<
package mathOperations;

import groovy.lang.Closure
import mathOperations.Math
import spock.lang.Specification

class MathSpec extends Specification {
    def objMath =new Math()

    def "API to test addition of two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.AddNumber is called with given values"
            def result =objMath.addNumber(a,b)
        then: "Result should be 15"
            result==15
    }

    def "API to test subrtaction of two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.subtractNumber is called with given values"
            def result =objMath.subtractNumber(a, b)
        then: "Result should be 5"
            result==5
    }

    def "API to test multiplication of two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.multiplyNumber is called with given values"
            def result =objMath.multiplyNumber(a,b)
        then: "Result should be 50"
            result==50
    }
    def "API to test division two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.divisionNumber is called with given values"
            def result =objMath.divisionNumber(a,b)
        then: "Result should be 2"
            result==2
    }

    def "API to test whether given both numbers are equal - Affirmative"() {
        given :"a and b"
            def a=10
            def b=10
        when: "Math.equalNumber is called with given values"
            def result =objMath.equalNumber(a,b)
        then: "It should return true"
            result==true
    }

    def "API to test whether given both numbers are equal - Negative"() {
        given :"a and b"
            def a=10
            def b=11
        when: "Math.equalNumber is called with given values"
            def result =objMath.equalNumber(a,b)
        then: "It should return false"
            result==false
    }
}
package mathOperations;

import groovy.lang.Closure
import mathOperations.Math
import spock.lang.Specification
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
import org.junit.Rule
import utils.QRCodeUtils
import org.powermock.api.mockito.PowerMockito
import static org.powermock.api.mockito.PowerMockito.mockStatic
import static org.mockito.BDDMockito.*

@PrepareForTest([QRCodeUtils.class])
class MathSpec extends Specification {
    def objMath =new Math()

    @Rule PowerMockRule powerMockRule = new PowerMockRule()

    def "API to test add two numbers"() {
        given :"a and b"
        def a=10
        def b=5
        when: "Math.AddNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.addNumber(a,b)
        then: "result should be 15"
        result==15
    }

    def "API to test subract two numbers"() {
        given :"a and b"
        def a=10
        def b=5
        when: "Math.subtractNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.subtractNumber(a, b)
        then: "result should be 5"
        result==5
    }
    def "API to test multiple two numbers"() {
        given :"a and b"
        def a=10
        def b=5
        when: "Math.multiplyNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.multiplyNumber(a,b)
        then: "result should be 50"
        result==50
    }
    def "API to test divide two numbers"() {
        given :"a and b"
        def a=10
        def b=5
        when: "Math.divisionNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.divisionNumber(a,b)
        then: "result should be 2"
        result==2
    }
    def "API to test modulo of a number"() {
        given :"a and b"
        def a=10
        def b=5
        when: "Math.moduloNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.moduloNumber(a,b)
        then: "result should be 0"
        result==0
    }

    def "API to test power of a number"() {
        given :"a and b"
        def a=10
        def b=2
        when: "Math.powerofNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.powerofNumber(a,b)
        then: "result should be 0"
        result==8
    }

    def "API to test numbers are equal -affirmative"() {
        given :"a and b"
        def a=10
        def b=10
        when: "Math.equalNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.equalNumber(a,b)
        then: "It should return true"
        result==true
    }

    def "API to test numbers are equal -negative"() {
        given :"a and b"
        def a=10
        def b=11
        when: "Math.equalNumber is call with given values"
        mockGetOTPAttemptStatus(5)
        def result =objMath.equalNumber(a,b)
        then: "It should return false"
        result==false
    }

    void mockGetOTPAttemptStatus(int status) {
        mockStatic(QRCodeUtils.class)
        when(QRCodeUtils.getOTPAttemptStatus(anyInt())).thenReturn(status)
    }
}
package mathOperations;

import groovy.lang.Closure
import java.sql.Statement
import mathOperations.Math
import spock.lang.Shared
import util.BaseSpec
import catalog.Root
import spock.lang.Ignore
import org.dbunit.ext.mssql.InsertIdentityOperation
import be.janbols.spock.extension.dbunit.DbUnit
import org.dbunit.IDatabaseTester

class MathSpec extends BaseSpec {

    @Shared root
    def objMath =new Math()

    @DbUnit(configure={
        IDatabaseTester it ->
        it.setUpOperation = InsertIdentityOperation.REFRESH
        it.tearDownOperation = InsertIdentityOperation.DELETE
    })
    def content =  {
        table1(id:99,MasterFile:'UnitTestFile',DataElementName:'test',DataElementDBColName:'TestDbCol',DataElementTableName:'TestTable')
        table2(id:99,MasterFile:'UnitTestFile',DataElementName:'test',DataElementDBColName:'TestDbCol',DataElementTableName:'TestTable')
        table3(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table4(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table5(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table6(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table7(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table8(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table9(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
        table10(id:99,Code:'T00.0',Status:'A',LongDesc:'Unit Testing')
    }

    def "API to test addition of two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.AddNumber is called with given values"
            def result =objMath.addNumber(a,b)
        then: "Result should be 15"
            result==15
    }

    def "API to test subrtaction of two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.subtractNumber is called with given values"
            def result =objMath.subtractNumber(a, b)
        then: "Result should be 5"
            result==5
    }

    def "API to test multiplication of two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.multiplyNumber is called with given values"
            def result =objMath.multiplyNumber(a,b)
        then: "Result should be 50"
            result==50
    }
    def "API to test division two numbers"() {
        given :"a and b"
            def a=10
            def b=5
        when: "Math.divisionNumber is called with given values"
            def result =objMath.divisionNumber(a,b)
        then: "Result should be 2"
            result==2
    }

    def "API to test whether given both numbers are equal - Affirmative"() {
        given :"a and b"
            def a=10
            def b=10
        when: "Math.equalNumber is called with given values"
            def result =objMath.equalNumber(a,b)
        then: "It should return true"
            result==true
    }

    def "API to test whether given both numbers are equal - Negative"() {
        given :"a and b"
            def a=10
            def b=11
        when: "Math.equalNumber is called with given values"
            def result =objMath.equalNumber(a,b)
        then: "It should return false"
            result==false
    }
}