/
pytest_konira.py
80 lines (51 loc) · 2.18 KB
/
pytest_konira.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pytest
from konira.runner import TestEnviron, get_methods, safe_skip_call
from konira.util import name_convertion, get_let_attrs, set_let_attrs
from konira.runner import get_classes
def pytest_addoption(parser):
"""Add option to collect Konira test cases"""
group = parser.getgroup('Konira DSL test cases')
group.addoption('--konira', action='store_true', help='Collects Konira test cases')
def pytest_collect_file(path, parent):
if parent.config.option.konira and path.basename.lower().startswith("case_"):
return KoniraFile(path, parent)
class KoniraFile(pytest.File):
def collect(self):
classes = get_classes(self.fspath.strpath, None)
for case in classes:
# Initialize the test class
suite = case()
# check test environment setup
environ = TestEnviron(suite)
methods = get_methods(suite, None)
if not methods: return
# Are we skipping?
if safe_skip_call(environ.set_skip_if):
return
let_attrs = get_let_attrs(suite)
# Set before all if any
environ.set_before_all()
for test in methods:
yield KoniraItem(str(test), self, suite, test, let_attrs)
# Set after all if any
environ.set_after_all()
class KoniraItem(pytest.Item):
def __init__(self, name, parent, case, spec, let_attrs):
super(KoniraItem, self).__init__(name, parent)
self.spec = spec
self.let_attrs = let_attrs
self.case = set_let_attrs(case, let_attrs)
def runtest(self):
# check test environment setup
environ = TestEnviron(self.case)
# Set before each if any
environ.set_before_each()
case = set_let_attrs(self.case, self.let_attrs)
getattr(case, self.spec)()
# Set after each if any
environ.set_after_each()
def reportinfo(self):
describe = "describe %s" % name_convertion(self.case.__class__.__name__)
it = name_convertion(self.name, capitalize=False)
describe_and_it = "%s ==>> %s" % (describe, it)
return self.fspath, 0, describe_and_it