-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6e8732b
Showing
8 changed files
with
701 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# See https://pre-commit.com for more information | ||
# See https://pre-commit.com/hooks.html for more hooks | ||
|
||
repos: | ||
|
||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v3.4.0 | ||
hooks: | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- id: check-yaml | ||
|
||
- repo: https://github.com/asottile/pyupgrade | ||
rev: v2.11.0 | ||
hooks: | ||
- id: pyupgrade | ||
args: ["--py38-plus"] | ||
|
||
- repo: https://github.com/asottile/seed-isort-config | ||
rev: v2.2.0 | ||
hooks: | ||
- id: seed-isort-config | ||
|
||
- repo: https://github.com/pre-commit/mirrors-isort | ||
rev: v5.8.0 | ||
hooks: | ||
- id: isort | ||
additional_dependencies: | ||
- toml | ||
|
||
- repo: https://github.com/ambv/black | ||
rev: 20.8b1 | ||
hooks: | ||
- id: black | ||
args: [--line-length=88, --safe] | ||
|
||
- repo: https://github.com/pycqa/pylint | ||
rev: pylint-2.7.4 | ||
hooks: | ||
- id: pylint | ||
args: [--extension-pkg-whitelist=django] | ||
additional_dependencies: ["Django"] | ||
|
||
- repo: https://github.com/pre-commit/mirrors-mypy | ||
rev: v0.812 | ||
hooks: | ||
- id: mypy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Sid Mitra | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# django-parallel-test | ||
|
||
Django test runner to run a test suite across different machines. | ||
|
||
It supports any CI system, like Heroku CI, Jenkins, Travis etc. | ||
|
||
## Quickstart | ||
|
||
To run the test suite on machine 1 out of a total 3, run the following: | ||
|
||
```shell | ||
pip install django-parallel-test | ||
|
||
CI_NODE_INDEX=0 CI_NODE_TOTAL=3 python manage.py test --testrunner=django_parallel_test.ParallelRunner | ||
``` | ||
|
||
### Heroku | ||
|
||
Additional steps are needed for Heroku to enable parallel dynos. | ||
|
||
- Update `app.json`, to add the quantity of dynos you want to run the tests on. | ||
|
||
To run your tests on 3 dynos in parallel, your config will look something like | ||
the following. | ||
|
||
```json | ||
... | ||
"test": { | ||
"formation": { | ||
"test": { | ||
"quantity": 3, | ||
} | ||
}, | ||
"scripts": { | ||
"test": "python manage.py test --testrunner=django_parallel_test.ParallelRunner", | ||
} | ||
} | ||
... | ||
``` | ||
|
||
Heroku already adds the config vars `CI_NODE_INDEX`_ and `CI_NODE_TOTAL`, so | ||
you do not need to add it explicitly | ||
|
||
- Commit and push to watch it in action! | ||
|
||
 | ||
|
||
### References | ||
|
||
- [Heroku CI Parallel Test Runs](https://devcenter.heroku.com/articles/heroku-ci-parallel-test-runs) | ||
- [Heroku CI Updates: Parallel Tests, CI API, and Automated UAT](https://blog.heroku.com/ci-parallel-tests) | ||
|
||
## Other CI | ||
|
||
- You can add the environment variables to your settings.py instead | ||
|
||
```python | ||
TEST_RUNNER = "django_parallel_test.HerokuParallelRunner" # or using --runner param | ||
CI_NODE_TOTAL = os.environ.get("CI_NODE_TOTAL", 1) # total number of CI dynos | ||
CI_NODE_INDEX = os.environ.get("CI_NODE_INDEX", 0) # index of the current dyno | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
""" | ||
Custom Django test runner | ||
""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
import os | ||
|
||
from django.conf import settings | ||
from django.test.runner import DiscoverRunner | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
__all__ = ["ParallelRunner"] | ||
|
||
|
||
class ParallelRunner(DiscoverRunner): | ||
""" | ||
Django test runner that runs the test suite in parallel on different machines | ||
It divides the entire test suite into different subsets and runs one particular | ||
subset on each of the dynos. It uses the variables `CI_NODE_TOTAL, CI_NODE_INDEX` in | ||
django settings or environment variables to select which subset to run on which | ||
machine. | ||
""" | ||
|
||
@property | ||
def ci_node_total(self) -> int: | ||
""" | ||
Return total number of dynos that will run the tests. | ||
""" | ||
if hasattr(settings, "CI_NODE_TOTAL"): | ||
return int(settings.CI_NODE_TOTAL) | ||
|
||
return int(os.environ.get("CI_NODE_TOTAL", 1)) | ||
|
||
@property | ||
def ci_node_index(self) -> int: | ||
""" | ||
Return index number of the current machine out of total machines available. | ||
""" | ||
if hasattr(settings, "CI_NODE_INDEX"): | ||
return int(settings.CI_NODE_INDEX) | ||
|
||
return int(os.environ.get("CI_NODE_INDEX", 0)) | ||
|
||
def split_suite(self, suite): | ||
""" | ||
Return a subset of the test classes in the suite to be run on the current node. | ||
""" | ||
|
||
def get_class_name(test_method): | ||
return str(test_method.__class__) | ||
|
||
# Get a list of all test classes we found in the suite. | ||
classes = sorted({get_class_name(test_method) for test_method in suite}) | ||
# Only select a subset of the test classes to be run on the node. | ||
classes_subset = [ | ||
test_class | ||
for idx, test_class in enumerate(classes) | ||
if idx % self.ci_node_total == self.ci_node_index | ||
] | ||
|
||
# Create a new test suite with only test methods from the subset of classes. | ||
suite_class = type(suite) | ||
new_suite = suite_class() | ||
count = 0 | ||
for test_method in suite: | ||
if get_class_name(test_method) in classes_subset: | ||
count += 1 | ||
new_suite.addTest(test_method) | ||
logger.info( | ||
f"{count} tests will be run on this node from the following classes:" | ||
f"\n{classes_subset}" | ||
) | ||
return new_suite | ||
|
||
def run_suite(self, suite, **kwargs): | ||
""" | ||
Run the entire test suite. | ||
""" | ||
if self.parallel > 1: | ||
logger.warning( | ||
"--parallel is not supported by this runner. " | ||
"Please use `quantity` key in app.json to increase number of " | ||
"nodes instead, if using Heroku" | ||
) | ||
raise ValueError("Unsupported parameter passed `--parallel`.") | ||
|
||
suite_with_subset = self.split_suite(suite) | ||
return super().run_suite(suite_with_subset, **kwargs) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.