Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
menny committed Jun 19, 2018
1 parent 49fa894 commit 03991e2
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A JUnit Filter for sharding your JUnit tests.
`master` coverage: [![codecov](https://codecov.io/gh/menny/JUnitTestsGrouping/branch/master/graph/badge.svg)](https://codecov.io/gh/menny/JUnitTestsGrouping)

## Usage
Using JUnit's `Filter` mechanism, `TestsGroupingFilter` will skip tests that are not in the execution group/shard.
Using JUnit's `Suite` or `Filter` mechanism, JUnitTestsGrouping will skip tests that are not in the execution group/shard.
Basically, it calculated the group index base on the test-class name and if this index equals the execution index
the test will execute, otherwise it will be filtered out.

Expand All @@ -17,11 +17,14 @@ repositories {
}
dependencies {
testCompile 'com.github.menny:JUnitTestsGrouping:0.2.0'
testImplementation 'com.github.menny:JUnitTestsGrouping:0.3.0'
}
```

### Setup - tests
There are two mechanisms in this library: `Filter` or `Suite`

### Setup - tests Filter
Add the filter to your JUnit test-runner. In this example, I created a custom `TestRunner`:
```
public class MyTestRunner extends RobolectricTestRunner {
Expand All @@ -32,15 +35,38 @@ public class MyTestRunner extends RobolectricTestRunner {
}
```

And you will need to set the number of groups to split the tests between, and set the current test group index (also, in your `build.gradle`):
### Setup - tests Suite
Create an empty class in your tests folder (at the root of the package), and annotate it with `ShardingSuite` and the hashing strategy:
```
@RunWith(ShardingSuite.class)
@ShardingSuite.ShardUsing(TestClassHashingStrategy.class)
public class MyTestsSuite {
}
```
Our `ShardingSuite` will scan your `ClassLoader` for all classes that have tests in them, and shard them using the `HashingStrategy` class
provided in `@ShardingSuite.ShardUsing`.
For Gradle, you will also need to tell Gradle that you want to run this Suite. From command line:
```
./gradlew -Dtest.single=MyTestsSuite clean test
```
Or, the preferred way, create a `Test` task:
```
task testWithSharding(type: Test) {
include '**/MyTestsSuite.class'
}
```

<br/>

For both mechanisms (Filter or Suite) you will need to set the number of groups to split the tests between, and set the current test group index (also, in your `build.gradle`):
```
systemProperties['TestsGroupingFilter_TEST_GROUP_TO_EXECUTE_SYSTEM_PROPERTY_KEY'] = System.getenv().getOrDefault('TEST_GROUP_INDEX', 0);
systemProperties['TestsGroupingFilter_TEST_GROUPS_COUNT_SYSTEM_PROPERTY_KEY'] = System.getenv().getOrDefault('TEST_GROUPS_COUNT', 1);
```

Where the environment variable`TEST_GROUP_INDEX` is the current test-group and `TEST_GROUPS_COUNT` is the total number of test groups.

## Example Scenario
## Example Scenario 1
So, an example could be:

* a Continuous Integration setup where you have 3 machines for running your tests.
Expand Down Expand Up @@ -73,9 +99,15 @@ public class AnnotationHashingStrategy extends SimpleHashingStrategyBase {

Similar strategy is implemented in `AnnotationHashingStrategy`.

## Example Scenario 2 - this library
In this library, we use the `Suite` mechanism in a non-standard way: we use the `HashingStrategy` to filter out test-classes that are
used as input in the tests - when our [strategy](https://github.com/menny/JUnitTestsGrouping/blob/master/src/test/java/net/evendanan/testgrouping/inputs/TestableHashingStrategy.java)
sees classes in the `inputs` package, it returns `-1`, which will ensure these classes are not executed.


## License
```
Copyright 2017 Menny Even-Danan
Copyright 2018 Menny Even-Danan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 03991e2

Please sign in to comment.