Simplify setup for the automated tests

This commit is contained in:
Bruno Alla 2020-02-29 17:15:35 +00:00
parent a0174840cb
commit 33f210abc4
5 changed files with 75 additions and 91 deletions

View File

@ -15,10 +15,6 @@ matrix:
include: include:
- name: Test results - name: Test results
script: tox -e py37 script: tox -e py37
- name: Run flake8 on result
script: tox -e flake8
- name: Run black on result
script: tox -e black
- name: Black template - name: Black template
script: tox -e black-template script: tox -e black-template
- name: Basic Docker - name: Basic Docker

View File

@ -2,6 +2,3 @@
addopts = -x --tb=short addopts = -x --tb=short
python_paths = . python_paths = .
norecursedirs = .tox .git */migrations/* */static/* docs venv */{{cookiecutter.project_slug}}/* norecursedirs = .tox .git */migrations/* */static/* docs venv */{{cookiecutter.project_slug}}/*
markers =
flake8: Run flake8 on all possible template combinations
black: Run black on all possible template combinations

View File

@ -11,7 +11,6 @@ flake8==3.7.9
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
tox==3.14.5 tox==3.14.5
pytest==5.3.5 pytest==5.3.5
pytest_cases==1.12.2
pytest-cookies==0.5.1 pytest-cookies==0.5.1
pytest-xdist==1.31.0 pytest-xdist==1.31.0
pyyaml==5.3 pyyaml==5.3

View File

@ -3,7 +3,6 @@ import re
import pytest import pytest
from cookiecutter.exceptions import FailedHookException from cookiecutter.exceptions import FailedHookException
from pytest_cases import fixture_plus
import sh import sh
import yaml import yaml
from binaryornot.check import is_binary from binaryornot.check import is_binary
@ -26,49 +25,62 @@ def context():
} }
@fixture_plus SUPPORTED_COMBINATIONS = [
@pytest.mark.parametrize("windows", ["y", "n"], ids=lambda yn: f"win:{yn}") {"open_source_license": "MIT"},
@pytest.mark.parametrize("use_docker", ["y", "n"], ids=lambda yn: f"docker:{yn}") {"open_source_license": "BSD"},
@pytest.mark.parametrize("use_celery", ["y", "n"], ids=lambda yn: f"celery:{yn}") {"open_source_license": "GPLv3"},
@pytest.mark.parametrize("use_mailhog", ["y", "n"], ids=lambda yn: f"mailhog:{yn}") {"open_source_license": "Apache Software License 2.0"},
@pytest.mark.parametrize("use_sentry", ["y", "n"], ids=lambda yn: f"sentry:{yn}") {"open_source_license": "Not open source"},
@pytest.mark.parametrize("use_compressor", ["y", "n"], ids=lambda yn: f"cmpr:{yn}") {"windows": "y"},
@pytest.mark.parametrize("use_drf", ["y", "n"], ids=lambda yn: f"drf:{yn}") {"windows": "n"},
@pytest.mark.parametrize( {"use_pycharm": "y"},
"use_whitenoise,cloud_provider", {"use_pycharm": "n"},
[ {"use_docker": "y"},
("y", "AWS"), {"use_docker": "n"},
("y", "GCP"), {"postgresql_version": "11.3"},
("y", "None"), {"postgresql_version": "10.8"},
("n", "AWS"), {"postgresql_version": "9.6"},
("n", "GCP"), {"postgresql_version": "9.5"},
# no whitenoise + no cloud provider is not supported {"postgresql_version": "9.4"},
], {"cloud_provider": "AWS", "use_whitenoise": "y"},
ids=lambda id: f"wnoise:{id[0]}-cloud:{id[1]}", {"cloud_provider": "AWS", "use_whitenoise": "n"},
) {"cloud_provider": "GCP", "use_whitenoise": "y"},
def context_combination( {"cloud_provider": "GCP", "use_whitenoise": "n"},
windows, {"cloud_provider": "None", "use_whitenoise": "y"},
use_docker, # Note: cloud_provider=None AND use_whitenoise=n is not supported
use_celery, {"use_drf": "y"},
use_mailhog, {"use_drf": "n"},
use_sentry, {"js_task_runner": "None"},
use_compressor, {"js_task_runner": "Gulp"},
use_whitenoise, {"custom_bootstrap_compilation": "y"},
use_drf, {"custom_bootstrap_compilation": "n"},
cloud_provider, {"use_compressor": "y"},
): {"use_compressor": "n"},
"""Fixture that parametrize the function where it's used.""" {"use_celery": "y"},
return { {"use_celery": "n"},
"windows": windows, {"use_mailhog": "y"},
"use_docker": use_docker, {"use_mailhog": "n"},
"use_compressor": use_compressor, {"use_sentry": "y"},
"use_celery": use_celery, {"use_sentry": "n"},
"use_mailhog": use_mailhog, {"use_whitenoise": "y"},
"use_sentry": use_sentry, {"use_whitenoise": "n"},
"use_whitenoise": use_whitenoise, {"use_heroku": "y"},
"use_drf": use_drf, {"use_heroku": "n"},
"cloud_provider": cloud_provider, {"ci_tool": "None"},
} {"ci_tool": "Travis"},
{"ci_tool": "Gitlab"},
{"keep_local_envs_in_vcs": "y"},
{"keep_local_envs_in_vcs": "n"},
{"debug": "y"},
{"debug": "n"},
]
UNSUPPORTED_COMBINATIONS = [{"cloud_provider": "None", "use_whitenoise": "n"}]
def _fixture_id(ctx):
"""Helper to get a user friendly test name from the parametrized context."""
return "-".join(f"{key}:{value}" for key, value in ctx.items())
def build_files_list(root_dir): def build_files_list(root_dir):
@ -81,9 +93,7 @@ def build_files_list(root_dir):
def check_paths(paths): def check_paths(paths):
"""Method to check all paths have correct substitutions, """Method to check all paths have correct substitutions."""
used by other tests cases
"""
# Assert that no match is found in any of the files # Assert that no match is found in any of the files
for path in paths: for path in paths:
if is_binary(path): if is_binary(path):
@ -95,13 +105,10 @@ def check_paths(paths):
assert match is None, msg.format(path) assert match is None, msg.format(path)
def test_project_generation(cookies, context, context_combination): @pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id)
""" def test_project_generation(cookies, context, context_override):
Test that project is generated and fully rendered. """Test that project is generated and fully rendered."""
result = cookies.bake(extra_context={**context, **context_override})
This is parametrized for each combination from ``context_combination`` fixture
"""
result = cookies.bake(extra_context={**context, **context_combination})
assert result.exit_code == 0 assert result.exit_code == 0
assert result.exception is None assert result.exception is None
assert result.project.basename == context["project_slug"] assert result.project.basename == context["project_slug"]
@ -112,14 +119,10 @@ def test_project_generation(cookies, context, context_combination):
check_paths(paths) check_paths(paths)
@pytest.mark.flake8 @pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id)
def test_flake8_passes(cookies, context_combination): def test_flake8_passes(cookies, context_override):
""" """Generated project should pass flake8."""
Generated project should pass flake8. result = cookies.bake(extra_context=context_override)
This is parametrized for each combination from ``context_combination`` fixture
"""
result = cookies.bake(extra_context=context_combination)
try: try:
sh.flake8(str(result.project)) sh.flake8(str(result.project))
@ -127,14 +130,10 @@ def test_flake8_passes(cookies, context_combination):
pytest.fail(e) pytest.fail(e)
@pytest.mark.black @pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id)
def test_black_passes(cookies, context_combination): def test_black_passes(cookies, context_override):
""" """Generated project should pass black."""
Generated project should pass black. result = cookies.bake(extra_context=context_override)
This is parametrized for each combination from ``context_combination`` fixture
"""
result = cookies.bake(extra_context=context_combination)
try: try:
sh.black("--check", "--diff", "--exclude", "migrations", f"{result.project}/") sh.black("--check", "--diff", "--exclude", "migrations", f"{result.project}/")
@ -187,9 +186,10 @@ def test_invalid_slug(cookies, context, slug):
assert isinstance(result.exception, FailedHookException) assert isinstance(result.exception, FailedHookException)
def test_no_whitenoise_and_no_cloud_provider(cookies, context): @pytest.mark.parametrize("invalid_context", UNSUPPORTED_COMBINATIONS)
"""It should not generate project if neither whitenoise or cloud provider are set""" def test_error_if_incompatible(cookies, context, invalid_context):
context.update({"use_whitenoise": "n", "cloud_provider": "None"}) """It should not generate project an incompatible combination is selected."""
context.update(invalid_context)
result = cookies.bake(extra_context=context) result = cookies.bake(extra_context=context)
assert result.exit_code != 0 assert result.exit_code != 0

12
tox.ini
View File

@ -1,18 +1,10 @@
[tox] [tox]
skipsdist = true skipsdist = true
envlist = py37,flake8,black,black-template envlist = py37,black-template
[testenv] [testenv]
deps = -rrequirements.txt deps = -rrequirements.txt
commands = pytest -m "not flake8" -m "not black" {posargs:./tests} commands = pytest {posargs:./tests}
[testenv:flake8]
deps = -rrequirements.txt
commands = pytest -m flake8 {posargs:./tests}
[testenv:black]
deps = -rrequirements.txt
commands = pytest -m black {posargs:./tests}
[testenv:black-template] [testenv:black-template]
deps = black deps = black