- Testing framework for Groovy & Java
- Developed in Groovy, inspired by RSpec
- It's a DSL designed to write better tests
- Every test extends from Specification (tests are specs)
- Tags like "expect" separate different blocks of the test
- Asserts are implicit
- The name of the test suite ends with *Spec
- The name of each test case can be a string
Simple unit test
- Given / When / Then blocks (BDD style)
- Can be used to write Unit, Integration, Functional & E2E tests
- Groovy code (type inference)
E2E test
- Fixture methods
Fixture methods
- Data driven tests (one variable example)
- Groovy code (dynamic)
Data driven test |
- @Unroll annotation
- String interpolation in the name of the test
@Unroll
- Without @Unroll
- With @Unroll & string interpolation
- If any execution fails, the test fails as a whole
- If any execution fails, only that test case will fail
@Unroll
- Data driven blocks with multiple variables
- Works with multiple variables
- Helpful to triangulate test
Data driven test ||
- Tells which test case fails
Data driven test ||
- Tells why it fails with a human readable error
Test output
- @Subject annotation
@Subject
- Mocking capabilities - Stub
Stubs
- Mocking capabilities - Mock
Mocks
- Mocking capabilities - WildChars
Mocking capabilities
Mocking capabilities
Mocking capabilities
Mocking capabilities
- Program the stub to return different values
Mocking capabilities
- Document test cases in a human readable way
Specifications as documentation
- The old method allows to work with the previous value
Old method
Testing exceptions
- Can assert that a specific type of exception is thrown
Testing exceptions
- Can make assertions on specific properties of the exception
@Stepwise
- Allows to write complex scenarios / user stories
@Ignore
- Excludes test from being executed
@IgnoreIf
- Excludes test executions based on specific conditions
- The opposite is @Requires
@IgnoreRest
- Excludes all tests not carrying this annotation
@Timeout
- Marks test as failed if it timeouts
@RestoreSystemProperties
- Restores system properties changed by the test automatically
Property testing (via Spock Genesis extension)
- Lazy data generators for property based testing
- Useful when attempting to test for a wide range of inputs
Reports