In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Spock test framework introduction and usage", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "Introduction and Usage of Spock Test Framework"!
Java projects using groovy simplified testing, java project testing framework spock tutorial
profile
Spock framework is a testing framework based on groovy syntax. Because groovy is used, it is more flexible to use than junit. The writing of test cases is easier to understand and clear at a glance.
If you have used junit, spock is easy to learn, you can learn by analogy.
use
Here is a direct example of spock usage:
1. Add dependency org.spockframework spock-core 1.2-groovy-2.4 test org.codehaus.groovy groovy-all 2.4.15 org.spockframework spock-spring 1.2-groovy-2.4 test 2. Life cycle of test method
When junit is used, it is mainly used to mark the method of the test class with the following comment:
@Test: mark the test method to run, there can be multiple @Test methods in a test class;@Before/@After: mark the method, will run before/after each test method;@ Before Class/@AfterClass: mark the method will run when the test class is initialized/destroyed;
Spock does not use the above annotation form, but the test class needs to inherit the Specification parent class, overriding the following methods in the parent class, you can customize the life cycle of the test method:
def setup() {} // run before every feature methoddef cleanup() {} // run after every feature methoddef setupSpec() {} // run before the first feature methoddef cleanupSpec() {} // run after the last feature method
So there is the following test code:
package com.yawn.spockimport spock.lang.Sharedimport spock.lang.Specification/** * spock test * @author yawn * 2019/6/10 9:43 */class CalculateSpec extends Specification { //initialization def setupSpec() { calculateService = new CalculateService() println ">>>>>> setupSpec" } def setup() { println ">>>>>> setup" } def cleanup() { println ">>>>>> cleanup" } def cleanupSpec() { println ">>>>>> cleanupSpec" } def "test life cycle"() { given: def a = 1 def b = 2 expect: a
< b println "test method finished!" }} 运行结果为:Life cycle method execution order for spock tests
3. Format of test methods
(1) given … expect …format:
The given statement block is conditional, expect is the expected result of the test, and true passes the test. The example above is in this format.
(2)given … when … then …
class CalculateSpec extends Specification { @Shared CalculateService calculateService //initialization def setupSpec() { calculateService = new CalculateService() } //Test method def "test plus 1"() { given: "Prepare data" def a = 1 def b = 2 when: "Test Method" def c = calculateService.plus(a, b) then: "Verification results" c == 4 - 1 }
Where the @Share annotation represents an instance shared by each test method. This instance is initialized in the setupSpec() method.
(3)when … then …
Same meaning as above.
(4)given … expect … where …
def "test1"() { given: "Prepare data" expect: "test method" z == calculateService.plus(x, y) where: "Verification Results" x | y || z 1 | 0 || 1 2 | 1 || 3}
Expect is the core test check statement block. where is an enumeration of multiple test cases, which is very intuitive.
The semantics of the above test method are: z is the result obtained by x and y after the method plus() operation, and now two sets of x,y, and z values are listed separately to test whether this relationship is satisfied.
Since there are two test cases, the plus() method runs twice here.
(5)expect … where …
Ibid.
(6)expect …
Ibid. Tests whether a single statement holds true.
4. where Test Case Enumeration Format:
After where, multiple test cases can be listed, with the following different formats:
// 1 Mapping format def "length of Spock's and his friends' names"() { expect: name.size() == length where: name | length "Spock">} // 2 Array list format (each array order can not be random) def "length of Yawn's and his friends' names"() { expect: name.size() == length where: name
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.