spock
Spock是一个Java测试框架,由GradleWare的软件工程师Peter Niederwieser于2008年创建,它可以促进BDD的发展。 利用这个
例如 ,一个故事可以定义为:
Story: Returns go to stockAs a store owner
In order to keep track of stock
I want to add items back to stock when they're returned.Scenario 1: Refunded items should be returned to stock
Given that a customer previously bought a black sweater from me
And I have three black sweaters in stock.
When he returns the black sweater for a refund
Then I should have four black sweaters in stock.Scenario 2: Replaced items should be returned to stock
Given that a customer previously bought a blue garment from me
And I have two blue garments in stock
And three black garments in stock.
When he returns the blue garment for a replacement in black
Then I should have three blue garments in stock
And three black garments in stock.
使用Spock,可以使用给定的相同时间, 格式将测试非常紧密地映射到方案规范。 在Spock中,我们可以将第一种情况实现为:
class SampleSpec extends Specification{def "Scenario 1: Refunded items should be returned to stock"() {given: "that a customer previously bought a black sweater from me"// ... code and: "I have three black sweaters in stock."// ... codewhen: "he returns the black sweater for a refund"// ... codethen: "I should have four black sweaters in stock."// ... code}
}
最好是确保测试方案需求到测试方案实现的准确映射。 如果我们可以输出的语法,我们可以走这条路
给定什么时候,然后从我们的测试。 Spock允许我们通过其扩展框架添加此功能。
因此,假设我们的BA真的很好奇,并且希望开发人员更加放心,他们坚持使用相同的给定时间,格式,并且代码是同步的。 他们希望轻松获得此信息。 开发人员可以通过首先定义此注释来提供此信息
import java.lang.annotation.*
import org.spockframework.runtime.extension.ExtensionAnnotation@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ExtensionAnnotation(ReportExtension)
@interface LogScenarioDescription {}
接下来是此实现:
import org.apache.log4j.Logger
import org.spockframework.runtime.AbstractRunListener
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
import org.spockframework.runtime.model.FeatureInfo
import org.spockframework.runtime.model.SpecInfoclass LogScenarioDescriptionExtension extends AbstractAnnotationDrivenExtension; {final static Logger logger = Logger.getLogger("scenarioLog." + ReportExtension.class);@Overridevoid visitSpecAnnotation(Report annotation, SpecInfo spec) {spec.addListener(new AbstractRunListener() {@Overridevoid afterFeature(FeatureInfo feature) {if (System.getEnv("logScenarios")) {logger.info("***SCENARIO TEST:*** " + feature.name)for (block in feature.blocks) {logger.info(block.kind);for (text in block.texts) {logger.info(text)}}}}})}
}
然后将其应用于测试
@LogScenarioDescription
class SampleSpec extends Specification{//...
执行测试后,将给出以下输出:
***SCENARIO TEST:*** Scenario 1: Refunded items should be returned to stock
GIVEN
that a customer previously bought a black sweater from me
AND
I have three black sweaters in stock.
WHEN
he returns the black sweater for a refund
THEN
I should have four black sweaters in stock.
使用以下log4j输出到特定的日志文件以进行方案记录:
log4j.rootLogger=INFO, stdoutlog4j.logger.scenarioLog.extension.custom=INFO, scenarioLoglog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%nlog4j.appender.scenarioLog=org.apache.log4j.FileAppender
log4j.appender.scenarioLog.File=logs/scenario.log
log4j.appender.scenarioLog.layout=org.apache.log4j.PatternLayout
log4j.appender.scenarioLog.layout.ConversionPattern=%m%n
现在您有了一个日志文件,您的BA,QA可以读取它! 这有助于建立协作和ATDD的敏捷文化,在此文化中,可以检查已达成一致的测试方案是否已实现。
翻译自: https://www.javacodegeeks.com/2017/07/outputting-given-extending-spock.html
spock