Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for unit tests of pytest plugins #104

Merged
merged 10 commits into from
May 28, 2019
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ config.yaml
.vscode
*.bak
openshift-install*
data/pull-secret
data/pull-secret
dist
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
graft conf
global-exclude *.py[cod] __pycache__ .gitignore *.html
Empty file removed __init__.py
Empty file.
14 changes: 7 additions & 7 deletions pytest_customization/ocscilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def pytest_configure(config):
config (pytest.config): Pytest config object

"""
init_ocsci_conf(config)
here = os.path.abspath(os.path.dirname(__file__))
init_ocsci_conf(
config,
default_config=os.path.join(here, "..", "conf/ocsci/default_config.yaml"),
)


def get_cli_param(config, name_of_param, default=None):
Expand Down Expand Up @@ -105,17 +109,13 @@ def process_cluster_cli_params(config):
ocsci_config.ENV_DATA['cluster_path'] = cluster_path


def init_ocsci_conf(
config,
default_config="conf/ocsci/default_config.yaml",
):
def init_ocsci_conf(config, default_config):
"""
Function to init the default config for OCS CI

Args:
config (pytest.config): Pytest config object
default_config (str): Default config data (default:
conf/ocsci/default_config.yaml)
default_config (str): Default config data

"""
custom_config = config.getoption('ocsci_conf')
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ envlist = py37,flake8
[testenv]
deps =
-rrequirements.txt
commands = {envpython} -m pytest --ignore=tests -c unittests/pytest.ini unittests
commands = {envpython} -m pytest --ignore=tests -c unittests/pytest.ini {posargs:unittests}

[testenv:flake8]
deps = flake8
commands = flake8
Expand Down
3 changes: 3 additions & 0 deletions unittests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

pytest_plugins = 'pytester'
71 changes: 71 additions & 0 deletions unittests/test_pytest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
# -*- coding: utf-8 -*-

import logging
import textwrap

import pytest


logger = logging.getLogger(__name__)


def test_pytest_works():
logger.info("This is prove that pytest works")


def test_help_message(testdir):
"""
Check that ``py.test --help`` output lists custom options of
ocscilib pytest plugin.
"""
result = testdir.runpytest('--help')
# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines([
'*--ocsci-conf*',
'*--cluster-conf*',
'*--cluster-path*',
'*--cluster-name*',
])


def test_simple(testdir):
"""
Make sure that pytest itself is not broken by running very simple test.
"""
# create a temporary pytest test module
testdir.makepyfile(textwrap.dedent("""\
def test_foo():
assert 1 == 1
"""))
# run pytest with the following cmd args
result = testdir.runpytest('-v')
# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines(['*::test_foo PASSED*'])
# make sure that that we get a '0' exit code for the testsuite
assert result.ret == 0


def test_config_parametrize(testdir):
"""
Parametrization via config values, use case described in
https://github.com/red-hat-storage/ocs-ci/pull/61#issuecomment-494866745
"""
# create a temporary pytest test module
testdir.makepyfile(textwrap.dedent("""\
import pytest

from ocsci import config as ocsci_config

@pytest.mark.parametrize("item", ocsci_config.DEMO)
def test_demo_parametrized_config(item):
assert item is not None
"""))
# create config file
conf_file = testdir.makefile(".yaml", textwrap.dedent("""\
DEMO:
- 1
- 2
"""))
# run pytest with the following cmd args
result = testdir.runpytest('-v', f'--ocsci-conf={conf_file}')
# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines([
'collecting*collected 2 items',
'*test_demo_parametrized_config?1? PASSED*',
'*test_demo_parametrized_config?2? PASSED*',
])
# make sure that that we get a '0' exit code for the testsuite
assert result.ret == 0