1 # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Copyright (c) 2013 Testrepository Contributors
18 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
19 # license at the users choice. A copy of both licenses are available in the
20 # project source as Apache-2.0 and BSD. You may not use this file except in
21 # compliance with one of these two licences.
23 # Unless required by applicable law or agreed to in writing, software
24 # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
25 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26 # license you chose for the specific language governing permissions and
27 # limitations under that license.
29 """setuptools/distutils command to run testr via setup.py
31 PBR will hook in the Testr class to provide "setup.py test" when
32 .testr.conf is present in the repository (see pbr/hooks/commands.py).
34 If we are activated but testrepository is not installed, we provide a
37 You can pass --coverage which will also export PYTHON='coverage run
38 --source <your package>' and automatically combine the coverage from
39 each testr backend test runner after the run completes.
43 from distutils import cmd
44 import distutils.errors
50 logger = logging.getLogger(__name__)
53 class TestrReal(cmd.Command):
55 description = "DEPRECATED: Run unit tests using testr"
58 ('coverage', None, "Replace PYTHON with coverage and merge coverage "
59 "from each testr worker."),
60 ('testr-args=', 't', "Run 'testr' with these args"),
61 ('omit=', 'o', "Files to omit from coverage calculations"),
62 ('coverage-package-name=', None, "Use this name to select packages "
63 "for coverage (one or more, "
65 ('slowest', None, "Show slowest test times after tests complete."),
66 ('no-parallel', None, "Run testr serially"),
67 ('log-level=', 'l', "Log level (default: info)"),
70 boolean_options = ['coverage', 'slowest', 'no_parallel']
72 def _run_testr(self, *args):
73 logger.debug("_run_testr called with args = %r", args)
74 return commands.run_argv([sys.argv[0]] + list(args),
75 sys.stdin, sys.stdout, sys.stderr)
77 def initialize_options(self):
78 self.testr_args = None
82 self.coverage_package_name = None
83 self.no_parallel = None
84 self.log_level = 'info'
86 def finalize_options(self):
87 self.log_level = getattr(
89 self.log_level.upper(),
91 logging.basicConfig(level=self.log_level)
92 logger.debug("finalize_options called")
93 if self.testr_args is None:
96 self.testr_args = self.testr_args.split()
98 self.omit = "--omit=%s" % self.omit
99 logger.debug("finalize_options: self.__dict__ = %r", self.__dict__)
102 """Set up testr repo, then run testr."""
103 logger.debug("run called")
105 warnings.warn('testr integration in pbr is deprecated. Please use '
106 'the \'testr\' setup command or call testr directly',
109 if not os.path.isdir(".testrepository"):
110 self._run_testr("init")
113 self._coverage_before()
114 if not self.no_parallel:
115 testr_ret = self._run_testr("run", "--parallel", *self.testr_args)
117 testr_ret = self._run_testr("run", *self.testr_args)
119 raise distutils.errors.DistutilsError(
120 "testr failed (%d)" % testr_ret)
122 print("Slowest Tests")
123 self._run_testr("slowest")
125 self._coverage_after()
127 def _coverage_before(self):
128 logger.debug("_coverage_before called")
129 package = self.distribution.get_name()
130 if package.startswith('python-'):
131 package = package[7:]
133 # Use this as coverage package name
134 if self.coverage_package_name:
135 package = self.coverage_package_name
136 options = "--source %s --parallel-mode" % package
137 os.environ['PYTHON'] = ("coverage run %s" % options)
138 logger.debug("os.environ['PYTHON'] = %r", os.environ['PYTHON'])
140 def _coverage_after(self):
141 logger.debug("_coverage_after called")
142 os.system("coverage combine")
143 os.system("coverage html -d ./cover %s" % self.omit)
144 os.system("coverage xml -o ./cover/coverage.xml %s" % self.omit)
147 class TestrFake(cmd.Command):
148 description = "Run unit tests using testr"
151 def initialize_options(self):
154 def finalize_options(self):
158 print("Install testrepository to run 'testr' command properly.")
162 from testrepository import commands