Skip to content

Commit

Permalink
Add an option to skip environment creation for tests (#5760)
Browse files Browse the repository at this point in the history
When iteration on tests, running the `dd_environment` every time can be
expensive. This adds a new `--skip-env` flag to `ddev test` to remove
that step, and run the tests directly.
  • Loading branch information
therve authored Feb 18, 2020
1 parent 6e44a59 commit a8401ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions datadog_checks_dev/datadog_checks/dev/_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

E2E_FIXTURE_NAME = 'dd_environment'
TESTING_PLUGIN = 'DDEV_TESTING_PLUGIN'
SKIP_ENVIRONMENT = 'DDEV_SKIP_ENV'


def e2e_active():
Expand Down
8 changes: 6 additions & 2 deletions datadog_checks_dev/datadog_checks/dev/plugin/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AGENT_COLLECTOR_SEPARATOR,
E2E_FIXTURE_NAME,
E2E_PARENT_PYTHON,
SKIP_ENVIRONMENT,
TESTING_PLUGIN,
e2e_active,
e2e_testing,
Expand Down Expand Up @@ -61,14 +62,17 @@ def datadog_agent():

@pytest.fixture(scope='session', autouse=True)
def dd_environment_runner(request):
# Skip the runner if the skip environment variable is specified
do_skip = os.getenv(SKIP_ENVIRONMENT) == 'true'

testing_plugin = os.getenv(TESTING_PLUGIN) == 'true'

# Do nothing if no e2e action is triggered and continue with tests
if not testing_plugin and not e2e_active(): # no cov
if not testing_plugin and not e2e_active() and not do_skip: # no cov
return
# If e2e tests are being run it means the environment has
# already been spun up so we prevent another invocation
elif e2e_testing(): # no cov
elif e2e_testing() or do_skip: # no cov
# Since the scope is `session` there should only ever be one definition
fixture_def = request._fixturemanager._arg2fixturedefs[E2E_FIXTURE_NAME][0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import click

from ..._env import E2E_PARENT_PYTHON
from ..._env import E2E_PARENT_PYTHON, SKIP_ENVIRONMENT
from ...subprocess import run_command
from ...utils import chdir, file_exists, get_ci_env_vars, remove_path, running_on_ci
from ..constants import get_root
Expand Down Expand Up @@ -40,6 +40,7 @@ def display_envs(check_envs):
@click.option('--passenv', help='Additional environment variables to pass down')
@click.option('--changed', is_flag=True, help='Only test changed checks')
@click.option('--cov-keep', is_flag=True, help='Keep coverage reports')
@click.option('--skip-env', is_flag=True, help='Skip environment creation and assume it is already running')
@click.option('--pytest-args', '-pa', help='Additional arguments to pytest')
@click.pass_context
def test(
Expand All @@ -61,6 +62,7 @@ def test(
passenv,
changed,
cov_keep,
skip_env,
pytest_args,
):
"""Run tests for Agent-based checks.
Expand Down Expand Up @@ -108,6 +110,10 @@ def test(
'DDEV_COV_MISSING': coverage_show_missing_lines,
}

if skip_env:
test_env_vars[SKIP_ENVIRONMENT] = 'true'
test_env_vars['TOX_TESTENV_PASSENV'] += f' {SKIP_ENVIRONMENT}'

if passenv:
test_env_vars['TOX_TESTENV_PASSENV'] += f' {passenv}'

Expand Down

0 comments on commit a8401ac

Please sign in to comment.