Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit feedcba

Browse files
authored
Merge pull request #606 from gleybersonandrade/tags
Create tags decorator to tests
2 parents b50e97c + 6d87c1c commit feedcba

File tree

108 files changed

+165
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+165
-94
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[run]
22
source = pyof
3+
omit = .eggs/*,.tox/*,*tests*,setup.py

setup.cfg

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ add-ignore = D105
1414

1515
[isort]
1616
known_first_party = pyof,tests
17-
multi_line_output = 4
17+
multi_line_output = 4
18+
19+
[tool:pytest]
20+
markers =
21+
small: marks tests as small
22+
medium: marks tests as medium
23+
large: marks tests as large

setup.py

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,39 @@ def finalize_options(self):
4545
"""Post-process options."""
4646

4747

48+
# pylint: disable=attribute-defined-outside-init, abstract-method
49+
class TestCommand(Command):
50+
"""Test tags decorators."""
51+
52+
user_options = [
53+
('size=', None, 'Specify the size of tests to be executed.'),
54+
('type=', None, 'Specify the type of tests to be executed.'),
55+
]
56+
57+
sizes = ('small', 'medium', 'large', 'all')
58+
types = ('unit', 'integration', 'e2e')
59+
60+
def get_args(self):
61+
"""Return args to be used in test command."""
62+
return '--size %s --type %s' % (self.size, self.type)
63+
64+
def initialize_options(self):
65+
"""Set default size and type args."""
66+
self.size = 'all'
67+
self.type = 'unit'
68+
69+
def finalize_options(self):
70+
"""Post-process."""
71+
try:
72+
assert self.size in self.sizes, ('ERROR: Invalid size:'
73+
f':{self.size}')
74+
assert self.type in self.types, ('ERROR: Invalid type:'
75+
f':{self.type}')
76+
except AssertionError as exc:
77+
print(exc)
78+
sys.exit(-1)
79+
80+
4881
class Cleaner(clean):
4982
"""Custom clean command to tidy up the project root."""
5083

@@ -58,15 +91,41 @@ def run(self):
5891
call('test -d docs && make -C docs/ clean', shell=True)
5992

6093

61-
class TestCoverage(SimpleCommand):
94+
class Test(TestCommand):
95+
"""Run all tests."""
96+
97+
description = 'run tests and display results'
98+
99+
def get_args(self):
100+
"""Return args to be used in test command."""
101+
markers = self.size
102+
if markers == "small":
103+
markers = 'not medium and not large'
104+
size_args = "" if self.size == "all" else "-m '%s'" % markers
105+
return '--addopts="tests/%s %s"' % (self.type, size_args)
106+
107+
def run(self):
108+
"""Run tests."""
109+
cmd = 'python setup.py pytest %s' % self.get_args()
110+
try:
111+
check_call(cmd, shell=True)
112+
except CalledProcessError as exc:
113+
print(exc)
114+
115+
116+
class TestCoverage(Test):
62117
"""Display test coverage."""
63118

64-
description = 'run unit tests and display code coverage'
119+
description = 'run tests and display code coverage'
65120

66121
def run(self):
67-
"""Run unittest quietly and display coverage report."""
68-
cmd = 'coverage3 run setup.py test && coverage3 report'
69-
check_call(cmd, shell=True)
122+
"""Run tests quietly and display coverage report."""
123+
cmd = 'coverage3 run setup.py pytest %s' % self.get_args()
124+
cmd += '&& coverage3 report'
125+
try:
126+
check_call(cmd, shell=True)
127+
except CalledProcessError as exc:
128+
print(exc)
70129

71130

72131
class DocTest(SimpleCommand):
@@ -80,17 +139,6 @@ def run(self):
80139
check_call(cmd, shell=True)
81140

82141

83-
class CITest(SimpleCommand):
84-
"""Run all CI tests."""
85-
86-
description = 'run all CI tests: unit and doc tests, linter'
87-
88-
def run(self):
89-
"""Run unit tests with coverage, doc tests and linter."""
90-
for command in TestCoverage, DocTest, Linter:
91-
self.run_command(command)
92-
93-
94142
class Linter(SimpleCommand):
95143
"""Lint Python source code."""
96144

@@ -107,6 +155,20 @@ def run(self):
107155
sys.exit(-1)
108156

109157

158+
class CITest(TestCommand):
159+
"""Run all CI tests."""
160+
161+
description = 'run all CI tests: unit and doc tests, linter'
162+
163+
def run(self):
164+
"""Run unit tests with coverage, doc tests and linter."""
165+
coverage_cmd = 'python setup.py coverage %s' % self.get_args()
166+
doctest_cmd = 'python setup.py doctest'
167+
lint_cmd = 'python setup.py lint'
168+
cmd = '%s && %s && %s' % (coverage_cmd, doctest_cmd, lint_cmd)
169+
check_call(cmd, shell=True)
170+
171+
110172
setup(name='python-openflow',
111173
version=__version__,
112174
description='Library to parse and generate OpenFlow messages',
@@ -127,7 +189,8 @@ def run(self):
127189
'clean': Cleaner,
128190
'coverage': TestCoverage,
129191
'doctest': DocTest,
130-
'lint': Linter
192+
'lint': Linter,
193+
'test': Test
131194
},
132195
zip_safe=False,
133196
classifiers=[

tests/unit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""python-openflow unit tests."""
File renamed without changes.

0 commit comments

Comments
 (0)