@@ -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+
4881class 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
72131class 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-
94142class 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+
110172setup (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 = [
0 commit comments