我目前使用的framework是Geb + Spock
我們需要TestSetId、TestCaseId 這兩個變數,才能將資料匯入Spira Test中,但 Spira Test 並沒有提供Spock的擴充API。
變通的辦法是Maven surefire會產出符合JUnit的報表格式,而Spira Test有提供 Extension For JUnit Automated Testing Framework,雖然並沒有完全符合我的需求,但其他部分可自行實作補足。
* SpiraTestCase
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@org.spockframework.runtime.extension.ExtensionAnnotation(SpockExtension.class)
public @interface SpiraTestCase {
public int id() default 0;
}
* SpiraTestSet
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@org.spockframework.runtime.extension.ExtensionAnnotation(SpockExtension.class)
public @interface SpiraTestSet {
public int id() default 0;
}
* SpockExtension
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension;
public class SpockExtension extends AbstractAnnotationDrivenExtension {
public void visitSpecAnnotation(java.lang.annotation.Annotation annotation, org.spockframework.runtime.model.SpecInfo spec) { /* compiled code */ }
private void sortFeaturesInDeclarationOrder(org.spockframework.runtime.model.SpecInfo spec) { /* compiled code */ }
private void includeFeaturesBeforeLastIncludedFeature(org.spockframework.runtime.model.SpecInfo spec) { /* compiled code */ }
private void skipFeaturesAfterFirstFailingFeature(org.spockframework.runtime.model.SpecInfo spec) { /* compiled code */ }
}
* SpiraIntegrationListener
public class SpiraIntegrationListener extends RunListener{
private Integer getSpiraTestCaseId(Description description){
Class<?> obj = description.getTestClass();
if (obj.isAnnotationPresent(SpiraTestCase.class)) {
Annotation annotation = obj.getAnnotation(SpiraTestCase.class);
SpiraTestCase spiraTestCase = (com.inflectra.spiratest.addons.junitextension.spock.SpiraTestCase) annotation;
return spiraTestCase.id();
}
return 0;
}
}
* pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<includes>
<include>*Spec.*</include>
</includes>
<systemPropertyVariables>
<geb.env>chrome</geb.env>
<geb.build.baseUrl>http://127.0.0.1</geb.build.baseUrl>
<geb.build.reportsDir>target/test-reports/geb</geb.build.reportsDir>
</systemPropertyVariables>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>com.inflectra.spiratest.addons.junitextension.SpiraIntegrationListener</value>
</property>
</properties>
</configuration>
</plugin>
* GebReportingSpec
@Stepwise
@SpiraTestCase(id=123)
@SpiraTestSet(id=1)
class FaqSpec extends GebReportingSpec