Karate
导包
< dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.12</ version> < scope> test</ scope> </ dependency> < dependency> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId> < version> 1.18.22</ version> </ dependency> < dependency> < groupId> commons-codec</ groupId> < artifactId> commons-codec</ artifactId> < version> 1.14</ version> </ dependency> < dependency> < groupId> org.apache.commons</ groupId> < artifactId> commons-lang3</ artifactId> < version> 3.10</ version> </ dependency> < dependency> < groupId> com.intuit.karate</ groupId> < artifactId> karate-junit5</ artifactId> < version> 1.1.0</ version> < scope> test</ scope> </ dependency> < dependency> < groupId> com.intuit.karate</ groupId> < artifactId> karate-core</ artifactId> < version> 1.1.0</ version> </ dependency> < dependency> < groupId> net.masterthought</ groupId> < artifactId> cucumber-reporting</ artifactId> < version> 5.6.1</ version> < scope> test</ scope> </ dependency> < dependency> < groupId> com.fasterxml.jackson.core</ groupId> < artifactId> jackson-core</ artifactId> < version> 2.12.4</ version> </ dependency> < dependency> < groupId> com.fasterxml.jackson.core</ groupId> < artifactId> jackson-databind</ artifactId> < version> 2.12.4</ version> </ dependency> < dependency> < groupId> com.alibaba</ groupId> < artifactId> fastjson</ artifactId> < version> 1.2.83</ version> </ dependency> < dependency> < groupId> org.slf4j</ groupId> < artifactId> slf4j-api</ artifactId> < version> 1.7.26</ version> < scope> compile</ scope> </ dependency> < dependency> < groupId> org.apache.avro</ groupId> < artifactId> avro</ artifactId> < version> 1.8.2</ version> </ dependency> < dependency> < groupId> cn.hutool</ groupId> < artifactId> hutool-all</ artifactId> < version> 5.8.10</ version> </ dependency> < build> < plugins> < plugin> < groupId> org.apache.maven.plugins</ groupId> < artifactId> maven-source-plugin</ artifactId> < version> 2.4</ version> < executions> < execution> < id> attach-sources</ id> < goals> < goal> jar</ goal> </ goals> </ execution> </ executions> </ plugin> </ plugins> </ build>
启动类
package toyota. it2 ; import com. intuit. karate. Results ;
import com. intuit. karate. Runner ;
import java. io. File ;
import java. nio. file. Paths ;
import java. util. ArrayList ;
import java. util. Collection ;
import java. util. List ;
import net. masterthought. cucumber. Configuration ;
import net. masterthought. cucumber. ReportBuilder ;
import org. apache. commons. io. FileUtils ;
import org. junit. Test ; public class TestRunner { @Test public void testParallel ( ) { Results results = Runner . path ( "src/test/resources/toyota/it2" ) . tags ( "@test" ) . karateEnv ( "test" ) . outputCucumberJson ( true ) . backupReportDir ( true ) . parallel ( 1 ) ; generateReport ( results. getReportDir ( ) ) ; } public static void generateReport ( String karateOutputPath) { String projectName = Paths . get ( "." ) . toAbsolutePath ( ) . normalize ( ) . getFileName ( ) . toString ( ) ; Collection < File > jsonFiles = FileUtils . listFiles ( new File ( karateOutputPath) , new String [ ] { "json" } , true ) ; List < String > jsonPaths = new ArrayList ( jsonFiles. size ( ) ) ; jsonFiles. forEach ( file -> jsonPaths. add ( file. getAbsolutePath ( ) ) ) ; Configuration config = new Configuration ( new File ( "target" ) , projectName) ; ReportBuilder reportBuilder = new ReportBuilder ( jsonPaths, config) ; reportBuilder. generateReports ( ) ; }
}
Karate
@test
Feature: TestBackground:# Test* def KarTest = Java.type('toyota.karate.KarTest')# 这个是读取的case描述的文件的位置* def caseList = read('classpath:toyota/it2/test/data/case.json')Scenario Outline: <id> - <desc>When def result = KarTest.insertUser()Then match result == <result>* KarTest.deleteUser()Examples:| caseList |
Data
[ { "id" : "001" , "desc" : "插入数据" , "result" : 1 } , { "id" : "002" , "desc" : "插入数据" , "result" : 1 }
]
启动
mvn clean test -DargLine = "-Dkarate.env=test" -U -e -Dtest = IT1Runner -Dkarate.options = "--tags @test"