- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们已经实现了 Spock + Db 单元 框架作为自动化单元测试的一部分。
我们现在有 2000 个测试用例(功能),用于 150 个 DbUnit 规范。
在这里,我们在数据库中添加所需的条目,然后测试每个方法的行为。
我们观察到执行这些测试用例大约需要 2 小时 30 分钟。
我有时间戳 setup
fixture 并在特征方法中添加了时间戳。以下是我的观察:
allergy.dao.AllergyFormDAOSpec > Get Allergy Form STANDARD_OUT
setup method execution started at : Fri Jan 12 19:00:42 IST 2018
allergy.dao.AllergyFormDAOSpec > API to get Allergy Form STANDARD_OUT
Feature method execution started at : Fri Jan 12 19:00:44 IST 2018
Feature method execution ended at: Fri Jan 12 19:00:45 IST 2018
Total time taken to run the one test case: 242
cleanup method execution started at : Fri Jan 12 19:00:45 IST 2018
Total time taken to run a feature method : 2531
在这里,我观察到在设置后加载特征方法平均需要 2-4 秒。但是,原始测试用例的执行时间不到一秒。
我想知道我是否可以获得有关此处可能延迟的内容的指示?因为,2000 个测试用例的 3 秒意味着 Spock 花费了将近 1 小时 30 分钟的时间,而不是真正的功能执行。
总而言之,我们希望减少每天运行 Spock 测试用例所花费的总时间。
规范
package allergy.dao
import java.util.Date
import org.dbunit.IDatabaseTester;
import org.dbunit.ext.mssql.InsertIdentityOperation;
import allergy.AllergyForm;
import be.janbols.spock.extension.dbunit.DbUnit;
import spock.lang.Shared
import util.MasterSpec
class AllergyFormDAOSpec extends MasterSpec {
def dao = new AllergyFormDAO();
@Shared Date timeStart1
@Shared Date timeEnd1
@DbUnit(configure={ IDatabaseTester it ->
it.setUpOperation = InsertIdentityOperation.REFRESH
it.tearDownOperation = InsertIdentityOperation.DELETE
})
def content = {
allergy_form(formId:99999,formName:'DummySpockForm',displayIndex:1,deleteFlag:0,is_biological:1)
allergy_form_facilities(id:99999,formId:99999,facilityid:2)
form_concentration(id:99999,formId:99999,name:'1:100',deleteflag:0,displayindex:1)
}
def setup(){
timeStart1 = new Date()
println "setup method execution started at : " + timeStart1
}
def "API to test delete Form facility"(){
def startTime = new Date()
println "Feature method execution started at : " + startTime
given:"form Id is given"
def formId = 99999
when:"delete form facilities"
def result =dao.deleteFormFacilities(null, formId)
then:"validate result"
(result>0)==true
def endTime = new Date()
println "Feature method execution ended at: " + endTime
println 'Total time taken to run the one test case: '+ (endTime.getTime() - startTime.getTime())
}
def cleanup() {
timeEnd1 = new Date()
println "cleanup method execution started at : " + timeEnd1
def difference = timeEnd1.time - timeStart1.time
println "Total time taken to run a fixture method : " + difference
}
}
MasterSpec
package util
import com.ecw.dao.SqlTranslator
import catalog.Root
import spock.lang.Shared
import spock.lang.Specification
import javax.sql.DataSource
/**
*/
class MasterSpec extends Specification {
@Shared
Properties properties = new Properties()
@Shared
public DataSource dataSource
@Shared
protected xmlDataSource = [:]
static int timeCntr = 0;
//setup is to read xml file's content in xmlDataSource Hashmap
def setup(){
//Get Running Class name without its package
def className = this.class.name.substring(this.class.name.lastIndexOf('.') + 1)
def resourceAnno = specificationContext.currentFeature.featureMethod.getAnnotation(FileResource)
if(resourceAnno != null){
def files = resourceAnno.xmlFiles()
def packageName = (this.class.package.name).replaceAll('\\.','/')
for(int i=0;i< files.length;i++){
def f = new File("src/test/resources/"+packageName+"/"+className+"/"+files[i])
def engine = new groovy.text.GStringTemplateEngine()
def template = engine.createTemplate(f).make(null)
def xmlString = template.toString()
//load the hashmap with file name as Key and its content in form of string as Value
xmlDataSource.put(files[i].split("\\.")[0],xmlString)
}
}
}
def setupSpec() {
Date timeStart = new Date()
File propertiesFile = new File('src/test/webapps/myApp/conf/connection.properties').withInputStream {
properties.load it
}
String strDBName = getPropertyValue("myApp.DBName")
if(strDBName.indexOf('?') > -1){
strDBName = strDBName.substring(0, strDBName.indexOf('?'))
}
String strServerName = getPropertyValue("myApp.DBHost");
if(strServerName.indexOf(':') > -1){
strServerName = strServerName.substring(0, strServerName.indexOf(':'))
}
String strUrl = getPropertyValue("myApp.DBUrl")
String strPort = strUrl.substring(strUrl.lastIndexOf(':') + 1)
//FOR MSSQL
System.setProperty("myApp.SkipJndi", "yes")
//dataSource = new JtdsDataSource()
Object 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 = (DataSource)newObject
dataSource.setDatabaseName(strDBName)
dataSource.setUser(getPropertyValue("myApp.DBUser"))
dataSource.setPassword(getPropertyValue("myApp.DBPassword"))
dataSource.setServerName(strServerName)
dataSource.setPortNumber(Integer.parseInt(strPort))
}
}
最佳答案
这个问题确实太宽泛了,没有提供足够的信息来给出合格的答案。但是,我可以说 Spock 本身应该非常快(不如“原始”JUnit 快,毕竟它仍然很不错,但对于测试来说确实足够快)。
从你的问题来看,你似乎怀疑 Spock 是一个瓶颈,所以,例如,您可以在启用 DbUnit 的测试旁边放置一个空的 Spock 测试并测量其执行情况,我可以向您保证,您获得的时间可以忽略不计。
所以我认为原因是在设置/清理期间,DbUnit 调用了一些与数据库相关的代码(可能是模式生成/表填充和/或删除)并且它花费了很多时间。因此,我的第二次尝试只是打印在测试期间运行的 SQL 查询,您可能会发现其中许多查询是在设置方法期间运行的。
另一个可能的原因是为了进行测试,在测试之前插入了太多数据。
还有一个可能的原因是您正在运行测试的数据库太慢(网络慢,数据库本身太忙)。
现在所有这些的解决方案是什么? :) 您可能想看看 Spring 测试数据访问层的方法 + 如何进行初始设置。由于远远超出了问题的范围,这里就不多说spring了,仅作为一个思路:
如果缓慢的原因是数据库服务器,那么(除了明显的建议,如“更改你的 RDBMS”)你可以尝试在同一台机器上的 docker 中运行一个数据库/甚至在你的测试开始本地使用之前启动一个数据库TestContainers .
关于unit-testing - Spock 与 DbUnit 测试用例缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48228080/
在特征方法中,在 when: 中指定特征 Action 。块,其结果在后续 then: 中得到测试堵塞。通常需要准备,这在 given: 中完成条款(或 setup: 或夹具方法)。包含前提条件同样有
我尝试使用 Spy 测试但没有成功。下面的类是一个 Sut。 public class FileManager { public int removeFiles(String director
我希望能够在运行一些自动化测试时记录 spock 功能名称和子句标签。这将有助于在使用 headless 浏览器进行自动化时调试测试问题,特别是 phantomjs。原因是,phantomjs 的行为
如何以编程方式跳过 Spock 框架中的测试?我知道我可以 annotate a test with @Ignore 跳过它,或使用 @IgnoreIf跳过基于环境变量等的测试。但是有没有办法运行任意
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
为简单起见,我们来看一个非常简单的类: public class TestingClass { public void method1(){ System.out.printl
Spock 只允许从 where 块访问静态变量。 是否有任何解决方法可以在 where 块中使用哪些实例变量? 最佳答案 您可以使用 @Shared 注释实例变量,见 http://spockfra
我正在使用 Spock 框架进行测试,一切都很好,直到今天;我不知道发生了什么。 Intellij 说“配置 Groovy sdk”所以我下载了 groovy sdk 2.4.9 并配置了它,但是在我
我正在为 grails 2.1.1 应用程序的一组现有测试添加第一个 spock 集成测试。使用以下方法运行时,测试运行和测试通过: grails test-app integration:spock
我过去曾在其他项目中使用旧版本的 robolectric 使用 robospock 和 electricspock 对 robolectric 进行过 spock 测试。我的新项目使用 robolec
我正在使用 Maven Surefire 插件运行一组 Spock 测试作为集成测试用例。我知道我们可以使用 @Shared 关键字在单个文件中跨规范的固定装置共享资源。 但是,是否可以在不同的规范文
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: def "findAll() Expected"(){ given: "The UR
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: @FailsWith(java.lang.AssertionError.class) def
我正在为我的插件创建 Spock 测试 project-plugin我的主要项目名称是 main-project正在使用 project-plugin作为插件。因此,当我为我的插件创建 Spock 测
在JUnit 3中,我可以这样获得当前正在运行的测试的名称: public class MyTest extends TestCase { public void testSomething(
我有一些类似Java的东西: public interface EventBus{ void fireEvent(GwtEvent event); } public class SaveCom
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
我遇到的问题是当我尝试在 then 中验证时阻止已抛出异常,并且已进行模拟调用。 看看下面的设置: class B { def b(A a) { a.a() } } c
我是一名优秀的程序员,十分优秀!