In this part you will see examples of shared state modifications in tests.
time
─────────────────────────────►
┌───────────┐ ┌───────────┐
│ test 1 │ │ test 2 │
└─┬───┬───┬─┘ └─┬───┬───┬─┘
│ │ │ │ │ │
│+ │? │- │+ │? │-
│a │a │a │a │a │a
│ │ │ │ │ │
▼ ▼ ▼ ▼ ▼ ▼
✓ ✓ ✓ ✓ ✓ ✓
symbol | description
+ | add entity (`given/when` block)
? | verify (`then` block)
- | remove entity/entities (`cleanup` block)
a | entity id
* | all entities
✓ | operation succeeded
! | operation failed
time
─────────────────────────────►
┌───────────┐
│ test 1 │
└─┬─────────┘
│
┌─┼─────────┐
│ │test 2 │
└─┼──┬──────┘
│ │
│+ │+
│a │a
│ │
▼ ▼
✓ !
time
─────────────────────────────►
┌───────────┐
│ test 1 │
└─┬───┬─┬───┘
│ │ │
│ │ │
┌─┼───┼─┼───┐
│ │test 2 │
└─┼─┬─┼─┼─┬─┘
│ │ │ │ │
│+│+│?│-│?
│a│b│a│*│b
│ │ │ │ │
▼ ▼ ▼ ▼ ▼
✓ ✓ ✓ ✓ !
time
─────────────────────────────►
┌───────────┐
│ test 1 │
└─┬─┬───────┘
│ │
┌─┼─┼───────┐
│ │test 2 │
└─┼─┼─┬─┬───┘
│ │ │ │
│+│?│+│?(count)
│a│a│b│*
│ │ │ │
▼ ▼ ▼ ▼
✓ ✓ ✓ !
Familiarize yourself with tests in this module.
- Run tests
./gradlew --rerun-tasks :part1.1-shared-state:test :part1.1-shared-state:createTestsExecutionReport
- Check reports
- Enable parallel execution
in
SpockConfig.groovy
- Run tests
again
./gradlew --rerun-tasks :part1.1-shared-state:test :part1.1-shared-state:createTestsExecutionReport --continue
Some tests failed. Eliminate shared state by using different name
in test cases.
- Set a unique
name
value in each test. - Run tests
again
./gradlew --rerun-tasks :part1.1-shared-state:test :part1.1-shared-state:createTestsExecutionReport --continue
- Check output
Sample output:
name = testName 3
name = testName 1
name = testName 2
- Add
private UniqueValueGenerator generator = RandomUniqueValueGenerator.instance
field. - Set
name
value insetup
usingname = generator.next()
- Remove
name
assignments in tests. - Run tests
again
./gradlew --rerun-tasks :part1.1-shared-state:test :part1.1-shared-state:createTestsExecutionReport --continue
- Check output
Sample output:
name = BM8LX
name = Kbc28
name = boR1b
- Add
private UniqueValueGenerator generator = NextIntValueGenerator.instance
field. - Set
name
value insetup
usingname = generator.next()
- Run tests
again
./gradlew --rerun-tasks :part1.1-shared-state:test :part1.1-shared-state:createTestsExecutionReport --continue
- Check output
Sample output:
name = 3
name = 1
name = 2
- Add
private UniqueValueGenerator generator = new TestNameUniqueValueGenerator(specificationContext)
field. - Set
name
value insetup
usingname = generator.next()
- Run tests
again
./gradlew --rerun-tasks :part1.1-shared-state:test :part1.1-shared-state:createTestsExecutionReport --continue
- Check output
Sample output:
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should remove file
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should create dir
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should create file