Skip to content

Commit

Permalink
adding conftest.py file
Browse files Browse the repository at this point in the history
Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
  • Loading branch information
prady0t committed Jun 15, 2024
1 parent 71b7008 commit b48392e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
25 changes: 25 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest


def pytest_addoption(parser):
parser.addoption(
"--examples", action="store_true", default=False, help="run examples tests"
)


def pytest_configure(config):
config.addinivalue_line("markers", "examples: mark test as an example")


def pytest_collection_modifyitems(config, items):
options = {
"examples": "examples",
}
selected_markers = [
marker for option, marker in options.items() if config.getoption(option)
]
if "examples" not in selected_markers:
skip_example = pytest.mark.skip(reason="Skipping example tests since --examples option is not provided")
for item in items:
if 'examples' in item.keywords:
item.add_marker(skip_example)
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def set_environment_variables(env_dict, session):
Sets environment variables for a nox Session object.
Parameters
-----------
---------
session : nox.Session
The session to set the environment variables for.
env_dict : dict
Expand Down Expand Up @@ -126,7 +126,7 @@ def run_scripts(session):
# is fixed
session.install("setuptools", silent=False)
session.install("-e", ".[all,dev]", silent=False)
session.run("python", "-m", "pytest", "tests/test_examples.py")
session.run("python", "-m", "pytest", "-v", "--examples", "tests/test_examples.py")


@nox.session(name="dev")
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ ignore = [
# NOTE: currently used only for notebook tests with the nbmake plugin.
[tool.pytest.ini_options]
minversion = "6"
markers = [
"examples: mark a test as an example test",
]
# Use pytest-xdist to run tests in parallel by default, exit with
# error if not installed
required_plugins = [
Expand Down
20 changes: 8 additions & 12 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import os
import runpy

from pathlib import Path
import pytest

ROOT_DIR = Path(os.path.join(
os.path.dirname(__file__), ".."))


class TestExamples:
"""
A class to test the example scripts.
"""

def list_of_files():
file_list = []
base_dir = os.path.join(
os.path.dirname(__file__), "..", "examples", "scripts"
)
for root, _, files in os.walk(base_dir):
for file in files:
if file.endswith(".py"):
file_list.append(os.path.join(root, file))
base_dir = ROOT_DIR.joinpath("examples", "scripts")
# Recursively find all python files inside examples/scripts
file_list = list(base_dir.rglob('*.py'))
return file_list



@pytest.mark.parametrize("files", list_of_files())
@pytest.mark.examples
def test_example_scripts(self, files):
runpy.run_path(files)

0 comments on commit b48392e

Please sign in to comment.