[PATCH 1/2] Start converting tests to pytest.
Amar Takhar
amar at rtems.org
Fri Mar 13 21:35:00 UTC 2020
This converts three files:
error.py:
- Check for exceptions and error messages
path.py:
- Test params and setting module variables
macros.py:
- Test class with setup and shared module access
This should cover the bulk of what is required to convert the rest to pytest.
Also removes the old test code from the modules themselves.
---
rtemstoolkit/conftest.py | 13 ++++++++
rtemstoolkit/error.py | 10 ------
rtemstoolkit/macros.py | 24 --------------
rtemstoolkit/path.py | 17 ----------
rtemstoolkit/tests/__init__.py | 0
rtemstoolkit/tests/test_error.py | 11 +++++++
rtemstoolkit/tests/test_macros.py | 41 +++++++++++++++++++++++
rtemstoolkit/tests/test_path.py | 54 +++++++++++++++++++++++++++++++
8 files changed, 119 insertions(+), 51 deletions(-)
create mode 100644 rtemstoolkit/conftest.py
create mode 100644 rtemstoolkit/tests/__init__.py
create mode 100644 rtemstoolkit/tests/test_error.py
create mode 100644 rtemstoolkit/tests/test_macros.py
create mode 100644 rtemstoolkit/tests/test_path.py
diff --git a/rtemstoolkit/conftest.py b/rtemstoolkit/conftest.py
new file mode 100644
index 0000000..a823a2e
--- /dev/null
+++ b/rtemstoolkit/conftest.py
@@ -0,0 +1,13 @@
+import pytest
+from sys import path
+import os
+
+
+# Always find the parent directory of rtemstoolkit/ no matter where
+# pytest is run.
+def pytest_runtest_setup(item):
+ path_toolkit = os.path.dirname(os.path.abspath(__file__))
+ path.append(path_toolkit[0:path_toolkit.rfind('/')])
+
+
+
diff --git a/rtemstoolkit/error.py b/rtemstoolkit/error.py
index e3a43c1..697b0a6 100644
--- a/rtemstoolkit/error.py
+++ b/rtemstoolkit/error.py
@@ -55,13 +55,3 @@ class exit(error):
"""Raise for to exit."""
def __init__(self):
pass
-
-if __name__ == '__main__':
- try:
- raise general('a general error')
- except general as gerr:
- print('caught:', gerr)
- try:
- raise internal('an internal error')
- except internal as ierr:
- print('caught:', ierr)
diff --git a/rtemstoolkit/macros.py b/rtemstoolkit/macros.py
index d8012e1..708107f 100644
--- a/rtemstoolkit/macros.py
+++ b/rtemstoolkit/macros.py
@@ -534,27 +534,3 @@ class macros:
def unlock_read_map(self):
self.read_map_locked = False
-
-if __name__ == "__main__":
- import copy
- import sys
- print(inspect.getfile(macros))
- m = macros()
- d = copy.copy(m)
- m['test1'] = 'something'
- if d.has_key('test1'):
- print('error: copy failed.')
- sys.exit(1)
- m.parse("[test]\n" \
- "test1: none, undefine, ''\n" \
- "name: none, override, 'pink'\n")
- print('set test:', m.set_read_map('test'))
- if m['name'] != 'pink':
- print('error: override failed. name is %s' % (m['name']))
- sys.exit(1)
- if m.has_key('test1'):
- print('error: map undefine failed.')
- sys.exit(1)
- print('unset test:', m.unset_read_map('test'))
- print(m)
- print(m.keys())
diff --git a/rtemstoolkit/path.py b/rtemstoolkit/path.py
index b15164b..7b5f64b 100644
--- a/rtemstoolkit/path.py
+++ b/rtemstoolkit/path.py
@@ -430,20 +430,3 @@ def get_humanize_size(path, depth = -1):
return "%5.3f%sB" % (size, unit)
size /= 1024.0
return "%.3f%sB" % (size, 'Y')
-
-if __name__ == '__main__':
- print(host('/a/b/c/d-e-f'))
- print(host('//a/b//c/d-e-f'))
- print(shell('/w/x/y/z'))
- print(basename('/as/sd/df/fg/me.txt'))
- print(dirname('/as/sd/df/fg/me.txt'))
- print(join('/d', 'g', '/tyty/fgfg'))
- windows = True
- print(host('/a/b/c/d-e-f'))
- print(host('//a/b//c/d-e-f'))
- print(shell('/w/x/y/z'))
- print(shell('w:/x/y/z'))
- print(basename('x:/sd/df/fg/me.txt'))
- print(dirname('x:/sd/df/fg/me.txt'))
- print(join('s:/d/', '/g', '/tyty/fgfg'))
- print(join('s:/d/e\\f/g', '/h', '/tyty/zxzx', '\\mm\\nn/p'))
diff --git a/rtemstoolkit/tests/__init__.py b/rtemstoolkit/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/rtemstoolkit/tests/test_error.py b/rtemstoolkit/tests/test_error.py
new file mode 100644
index 0000000..85ceeae
--- /dev/null
+++ b/rtemstoolkit/tests/test_error.py
@@ -0,0 +1,11 @@
+import pytest
+from rtemstoolkit.error import general, internal
+
+def test_general():
+ with pytest.raises(general, match=r"^error: test_general$"):
+ raise general("test_general")
+
+
+def test_insernal():
+ with pytest.raises(internal, match=r"^internal error: test_internal$"):
+ raise internal("test_internal")
diff --git a/rtemstoolkit/tests/test_macros.py b/rtemstoolkit/tests/test_macros.py
new file mode 100644
index 0000000..aa9b80a
--- /dev/null
+++ b/rtemstoolkit/tests/test_macros.py
@@ -0,0 +1,41 @@
+import pytest
+
+from rtemstoolkit.macros import macros
+
+
+class TestClass:
+ @classmethod
+ def setup_class(cls):
+ import copy
+ import sys
+ cls.m = macros()
+ cls.d = copy.copy(cls.m)
+
+
+ def test_copy_failure(self):
+ self.m['test1'] = 'something'
+ assert "test1" not in self.d
+
+
+ def test_set_tests(self):
+ assert self.m.parse("[test]\n" \
+ "test1: none, undefine, ''\n" \
+ "name: none, override, 'pink'\n") is None
+
+ assert self.m.set_read_map("test")
+
+
+ def test_check_override(self):
+ if self.m["name"] != "pink":
+ raise Exception("error: override failed. name is %s" % self.m["name"])
+
+
+ def test_undefine(self):
+ assert "test1" not in self.m
+
+
+ def test_unset(self):
+ assert "name" in self.m
+ assert self.m.unset_read_map("test")
+ assert "name" not in self.m
+
diff --git a/rtemstoolkit/tests/test_path.py b/rtemstoolkit/tests/test_path.py
new file mode 100644
index 0000000..2ce9acb
--- /dev/null
+++ b/rtemstoolkit/tests/test_path.py
@@ -0,0 +1,54 @@
+import pytest
+
+from rtemstoolkit import path
+
+
+rtems_windows = pytest.mark.parametrize("windows", [False, True], ids=["Windows=False", "Windows=True"])
+
+
+ at rtems_windows
+def test_host(windows):
+ path.windows = windows
+
+ if windows:
+ path_str = "a:\\b\\c\\d-e-f"
+ else:
+ path_str = "/a/b/c/d-e-f"
+
+ assert path.host('//a/b/c/d-e-f') == path_str
+
+
+ at rtems_windows
+def test_shell(windows):
+ path.windows = windows
+ assert path.shell('/w/x/y/z') == "/w/x/y/z"
+
+ at rtems_windows
+def test_basename(windows):
+ path.windows = windows
+ assert path.basename('/as/sd/df/fg/me.txt') == "me.txt"
+
+ if windows:
+ assert path.basename('x:/sd/df/fg/me.txt') == "me.txt"
+
+
+ at rtems_windows
+def test_dirname(windows):
+ path.windows = windows
+ assert path.dirname('/as/sd/df/fg/me.txt') == "/as/sd/df/fg"
+
+ if windows:
+ assert path.dirname('x:/sd/df/fg/me.txt') == "/x/sd/df/fg"
+
+
+ at rtems_windows
+def test_join(windows):
+ path.windows = windows
+ assert path.join('/d', 'g', '/tyty/fgfg') == "/d/g/tyty/fgfg"
+
+ if windows:
+ assert path.join('s:/d/e\\f/g', '/h', '/tyty/zxzx', '\\mm\\nn/p') == "/s/d/e/f/g/h/tyty/zxzx/mm/nn/p"
+ assert path.join('s:/d/', '/g', '/tyty/fgfg') == "/s/d/g/tyty/fgfg"
+
+
+
--
2.25.0
More information about the devel
mailing list