From a99f9b76f575ea062eac5cc1280edfb2afc8c96b Mon Sep 17 00:00:00 2001 From: lukas leufen <l.leufen@fz-juelich.de> Date: Mon, 25 Nov 2019 15:10:23 +0100 Subject: [PATCH] shift tests for RunEnvironment, PyTestRegex class is now part of the helpers functions --- src/helpers.py | 15 ++++++++ test/test_modules.py | 41 +++------------------- test/test_modules/__init__.py | 0 test/test_modules/test_experiment_setup.py | 4 +++ test/test_modules/test_run_environment.py | 31 ++++++++++++++++ 5 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 test/test_modules/__init__.py create mode 100644 test/test_modules/test_experiment_setup.py create mode 100644 test/test_modules/test_run_environment.py diff --git a/src/helpers.py b/src/helpers.py index 83c5dcf4..1b635a2e 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -1,3 +1,5 @@ +import re + __author__ = 'Lukas Leufen' __date__ = '2019-10-21' @@ -180,3 +182,16 @@ def set_experiment_name(experiment_date=None, experiment_path=None): else: experiment_path = os.path.abspath(experiment_path) return experiment_name, experiment_path + + +class PyTestRegex: + """Assert that a given string meets some expectations.""" + + def __init__(self, pattern: str, flags: int = 0): + self._regex = re.compile(pattern, flags) + + def __eq__(self, actual: str) -> bool: + return bool(self._regex.match(actual)) + + def __repr__(self) -> str: + return self._regex.pattern diff --git a/test/test_modules.py b/test/test_modules.py index 8437c912..b2add462 100644 --- a/test/test_modules.py +++ b/test/test_modules.py @@ -1,45 +1,12 @@ import logging -from src.modules import run, PreProcessing -from src.helpers import TimeTracking +from src.modules.pre_processing import PreProcessing +from src.helpers import TimeTracking, PyTestRegex from src.modules.experiment_setup import ExperimentSetup from src.data_generator import DataGenerator -import re import mock import numpy as np -class pytest_regex: - """Assert that a given string meets some expectations.""" - - def __init__(self, pattern, flags=0): - self._regex = re.compile(pattern, flags) - - def __eq__(self, actual): - return bool(self._regex.match(actual)) - - def __repr__(self): - return self._regex.pattern - - -class TestRun: - - def test_enter_exit(self, caplog): - caplog.set_level(logging.INFO) - with run() as r: - assert caplog.record_tuples[-1] == ('root', 20, 'run started') - assert isinstance(r.time, TimeTracking) - r.do_stuff(0.1) - assert caplog.record_tuples[-1] == ('root', 20, pytest_regex(r"run finished after \d+\.\d+s")) - - def test_init_del(self, caplog): - caplog.set_level(logging.INFO) - r = run() - assert caplog.record_tuples[-1] == ('root', 20, 'run started') - r.do_stuff(0.2) - del r - assert caplog.record_tuples[-1] == ('root', 20, pytest_regex(r"run finished after \d+\.\d+s")) - - class TestPreProcessing: def test_init(self, caplog): @@ -49,7 +16,7 @@ class TestPreProcessing: pre = PreProcessing(setup) assert caplog.record_tuples[0] == ('root', 20, 'PreProcessing started') assert caplog.record_tuples[1] == ('root', 20, 'check valid stations started') - assert caplog.record_tuples[-1] == ('root', 20, pytest_regex(r'run for \d+\.\d+s to check 5 station\(s\)')) + assert caplog.record_tuples[-1] == ('root', 20, PyTestRegex(r'run for \d+\.\d+s to check 5 station\(s\)')) def test_run(self): pre_processing = object.__new__(PreProcessing) @@ -73,7 +40,7 @@ class TestPreProcessing: valids = pre.check_valid_stations(pre.setup.__dict__, kwargs, pre.setup.stations) assert valids == pre.setup.stations assert caplog.record_tuples[0] == ('root', 20, 'check valid stations started') - assert caplog.record_tuples[1] == ('root', 20, pytest_regex(r'run for \d+\.\d+s to check 5 station\(s\)')) + assert caplog.record_tuples[1] == ('root', 20, PyTestRegex(r'run for \d+\.\d+s to check 5 station\(s\)')) def test_update_kwargs(self): args = {"testName": {"testAttribute": "TestValue", "optional": "2019-11-21"}} diff --git a/test/test_modules/__init__.py b/test/test_modules/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/test_modules/test_experiment_setup.py b/test/test_modules/test_experiment_setup.py new file mode 100644 index 00000000..65ec052b --- /dev/null +++ b/test/test_modules/test_experiment_setup.py @@ -0,0 +1,4 @@ + +import pytest + + diff --git a/test/test_modules/test_run_environment.py b/test/test_modules/test_run_environment.py new file mode 100644 index 00000000..ce5f995e --- /dev/null +++ b/test/test_modules/test_run_environment.py @@ -0,0 +1,31 @@ +import logging + +from src.helpers import TimeTracking, PyTestRegex +from src.modules.run_environment import RunEnvironment + + +class TestRunEnvironment: + + def test_enter(self, caplog): + caplog.set_level(logging.INFO) + with RunEnvironment() as r: + assert caplog.record_tuples[-1] == ('root', 20, 'RunEnvironment started') + assert isinstance(r.time, TimeTracking) + + def test_exit(self, caplog): + caplog.set_level(logging.INFO) + with RunEnvironment() as r: + r.do_stuff(0.1) + assert caplog.record_tuples[-1] == ('root', 20, PyTestRegex(r"RunEnvironment finished after \d+\.\d+s")) + + def test_init(self, caplog): + caplog.set_level(logging.INFO) + r = RunEnvironment() + assert caplog.record_tuples[-1] == ('root', 20, 'RunEnvironment started') + + def test_del(self, caplog): + caplog.set_level(logging.INFO) + r = RunEnvironment() + r.do_stuff(0.2) + del r + assert caplog.record_tuples[-1] == ('root', 20, PyTestRegex(r"RunEnvironment finished after \d+\.\d+s")) -- GitLab