Skip to content

Commit d6c948b

Browse files
committed
tests: Initial port to pytest
This is just switching to pytest conventions and command line arguments, but not using much more than that yet
1 parent 7b9e870 commit d6c948b

16 files changed

Lines changed: 110 additions & 129 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
MANIFEST
44
dist
55
build
6+
.cache
67
.coverage
78
.tox
89

CONTRIBUTING.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@ dependencies, running the command line from git is as simple as doing:
99

1010
# Running tests
1111

12-
Once you have already activated an environment, you can use the following.
12+
Our test suite uses pytest. If your system has dependencies already, the
13+
quick unit test suite is invoked simply with:
1314

14-
## Basic unit test suite
15-
16-
python setup.py test
15+
pytest
1716

1817
## Read-Only Functional tests
19-
There are more comprehensive tests that are disabled by default. Readonly
20-
functional tests that run against several public bugzilla instances. No
21-
login account is required:
2218

23-
python setup.py test --ro-functional
19+
There are more comprehensive, readonly functional tests that run against
20+
several public bugzilla instances, but they are not run by default. No
21+
login account is required. Run them with:
22+
23+
pytest --ro-functional
2424

2525
## Read/Write Functional Tests.
2626

27-
Before running rw-functional tests, make sure you have logged into bugzilla
28-
using. These currently run against the test bugzilla instance at
29-
partner-bugzilla.redhat.com, and requires a valid login there:
27+
Read/Write functional tests use partner-bugzilla.redhat.com, which is a
28+
bugzilla instance specifically for this type of testing. Data is occasionally
29+
hard synced with regular bugzilla.redhat.com, and all local edits are
30+
removed. Login accounts are also synced. If you want access to
31+
partner-bugzilla.redhat.com, sign up for a regular bugzilla.redhat.com login
32+
and wait for the next sync period.
33+
34+
Before running these tests, you'll need to cache login credentials.
35+
Example:
3036

3137
./bugzilla-cli --bugzilla=partner-bugzilla.redhat.com --username=$USER login
32-
python setup.py test --rw-functional
38+
pytest --rw-functional
3339

3440
## Testing across python versions
3541
To test all supported python versions, run tox using any of the following.
@@ -43,7 +49,7 @@ To test all supported python versions, run tox using any of the following.
4349

4450
To test for pylint or pycodestyle violations, you can run:
4551

46-
python setup.py pylint
52+
./setup.py pylint
4753

4854
Note: This expects that you already have pylint and pycodestyle installed.
4955

bugzilla/_cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040

4141
DEFAULT_BZ = 'https://bugzilla.redhat.com/xmlrpc.cgi'
4242

43-
_is_unittest = bool(os.getenv("__BUGZILLA_UNITTEST"))
44-
_is_unittest_debug = bool(os.getenv("__BUGZILLA_UNITTEST_DEBUG"))
4543
format_field_re = re.compile("%{([a-z0-9_]+)(?::([^}]*))?}")
4644

4745
log = getLogger(bugzilla.__name__)
@@ -51,6 +49,14 @@
5149
# Util helpers #
5250
################
5351

52+
def _is_unittest():
53+
return bool(os.getenv("__BUGZILLA_UNITTEST"))
54+
55+
56+
def _is_unittest_debug():
57+
return bool(os.getenv("__BUGZILLA_UNITTEST_DEBUG"))
58+
59+
5460
def to_encoding(ustring):
5561
string = ''
5662
if isinstance(ustring, basestring):
@@ -62,7 +68,7 @@ def to_encoding(ustring):
6268
return string
6369

6470
preferred = locale.getpreferredencoding()
65-
if _is_unittest:
71+
if _is_unittest():
6672
preferred = "UTF-8"
6773
return string.encode(preferred, 'replace')
6874

@@ -116,7 +122,7 @@ def setup_logging(debug, verbose):
116122
else:
117123
log.setLevel(WARN)
118124

119-
if _is_unittest_debug:
125+
if _is_unittest_debug():
120126
log.setLevel(DEBUG)
121127

122128

python-bugzilla.spec

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ BuildArch: noarch
1818
BuildRequires: python2-devel
1919
BuildRequires: python-requests
2020
BuildRequires: python-setuptools
21+
BuildRequires: pytest
2122

2223
%if 0%{?with_python3}
2324
BuildRequires: python3-devel
2425
BuildRequires: python3-requests
2526
BuildRequires: python3-setuptools
27+
BuildRequires: python3-pytest
2628
%endif # if with_python3
2729

2830
%global _description\
@@ -112,7 +114,10 @@ done
112114

113115

114116
%check
115-
%{__python2} setup.py test
117+
pytest
118+
%if 0%{?with_python3}
119+
pytest-3
120+
%endif
116121

117122

118123

setup.py

Lines changed: 5 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import glob
66
import os
77
import sys
8-
import unittest
98

109
from distutils.core import Command
1110
from setuptools import setup
@@ -28,114 +27,16 @@ def get_version():
2827

2928

3029
class TestCommand(Command):
31-
user_options = [
32-
("ro-functional", None,
33-
"Run readonly functional tests against actual bugzilla instances. "
34-
"This will be very slow."),
35-
("rw-functional", None,
36-
"Run read/write functional tests against actual bugzilla instances. "
37-
"As of now this only runs against partner-bugzilla.redhat.com, "
38-
"which requires an RH bugzilla account with cached cookies. "
39-
"This will also be very slow."),
40-
("only=", None,
41-
"Run only tests whose name contains the passed string"),
42-
("redhat-url=", None,
43-
"Redhat bugzilla URL to use for ro/rw_functional tests"),
44-
("debug", None,
45-
"Enable python-bugzilla debug output. This may break output "
46-
"comparison tests."),
47-
]
30+
user_options = []
4831

4932
def initialize_options(self):
50-
self.ro_functional = False
51-
self.rw_functional = False
52-
self.only = None
53-
self.redhat_url = None
54-
self.debug = False
55-
33+
pass
5634
def finalize_options(self):
5735
pass
5836

5937
def run(self):
60-
os.environ["__BUGZILLA_UNITTEST"] = "1"
61-
62-
try:
63-
import coverage
64-
usecov = int(coverage.__version__.split(".")[0]) >= 3
65-
except:
66-
usecov = False
67-
68-
if usecov:
69-
cov = coverage.coverage(omit=[
70-
"/*/tests/*", "/usr/*", "*dev-env*", "*.tox/*"])
71-
cov.erase()
72-
cov.start()
73-
74-
testfiles = []
75-
for t in glob.glob(os.path.join(os.getcwd(), 'tests', '*.py')):
76-
if t.endswith("__init__.py"):
77-
continue
78-
79-
base = os.path.basename(t)
80-
if (base == "ro_functional.py" and not self.ro_functional):
81-
continue
82-
83-
if (base == "rw_functional.py" and not self.rw_functional):
84-
continue
85-
86-
testfiles.append('.'.join(['tests', os.path.splitext(base)[0]]))
87-
88-
89-
if hasattr(unittest, "installHandler"):
90-
try:
91-
unittest.installHandler()
92-
except:
93-
print("installHandler hack failed")
94-
95-
import tests as testsmodule
96-
testsmodule.REDHAT_URL = self.redhat_url
97-
if self.debug:
98-
import logging
99-
import bugzilla
100-
logging.getLogger(bugzilla.__name__).setLevel(logging.DEBUG)
101-
os.environ["__BUGZILLA_UNITTEST_DEBUG"] = "1"
102-
103-
tests = unittest.TestLoader().loadTestsFromNames(testfiles)
104-
if self.only:
105-
newtests = []
106-
for suite1 in tests:
107-
for suite2 in suite1:
108-
for testcase in suite2:
109-
if self.only in str(testcase):
110-
newtests.append(testcase)
111-
112-
if not newtests:
113-
print("--only didn't find any tests")
114-
sys.exit(1)
115-
116-
tests = unittest.TestSuite(newtests)
117-
print("Running only:")
118-
for test in newtests:
119-
print("%s" % test)
120-
print()
121-
122-
123-
verbosity = 1
124-
if self.ro_functional or self.rw_functional:
125-
verbosity = 2
126-
t = unittest.TextTestRunner(verbosity=verbosity)
127-
128-
result = t.run(tests)
129-
130-
if usecov:
131-
cov.stop()
132-
cov.save()
133-
134-
err = int(bool(len(result.failures) > 0 or
135-
len(result.errors) > 0))
136-
if not err and usecov:
137-
cov.report(show_missing=False)
138-
sys.exit(err)
38+
print("\n* Tests are now run with the 'pytest' tool.\n"
39+
"* See CONTRIBUTING.md for details.")
13940

14041

14142
class PylintCommand(Command):
@@ -206,6 +107,7 @@ def _parse_requirements(fname):
206107
ret.append(line)
207108
return ret
208109

110+
209111
setup(name='python-bugzilla',
210112
version=get_version(),
211113
description='Bugzilla XMLRPC access module',

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# additional packages needed for testing
2-
coverage
2+
pytest

tests/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
from bugzilla import Bugzilla, RHBugzilla, _cli
1616

1717

18-
# This is overwritten by python setup.py test --redhat-url, and then
19-
# used in ro/rw tests
20-
REDHAT_URL = None
18+
class _CLICONFIG(object):
19+
def __init__(self):
20+
self.REDHAT_URL = None
21+
22+
23+
CLICONFIG = _CLICONFIG()
24+
os.environ["__BUGZILLA_UNITTEST"] = "1"
2125

2226

2327
def make_bz(version, *args, **kwargs):

tests/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
import os
3+
4+
import tests
5+
6+
import bugzilla
7+
8+
9+
# pytest plugin adding custom options. Hooks are documented here:
10+
# https://docs.pytest.org/en/latest/writing_plugins.html
11+
12+
def pytest_addoption(parser):
13+
parser.addoption("--ro-functional", action="store_true", default=False,
14+
help=("Run readonly functional tests against actual "
15+
"bugzilla instances. This will be very slow."))
16+
parser.addoption("--rw-functional", action="store_true", default=False,
17+
help=("Run read/write functional tests against actual bugzilla "
18+
"instances. As of now this only runs against "
19+
"partner-bugzilla.redhat.com, which requires an RH "
20+
"bugzilla account with cached login creds. This will "
21+
"also be very slow."))
22+
parser.addoption("--redhat-url",
23+
help="Redhat bugzilla URL to use for ro/rw_functional tests")
24+
parser.addoption("--pybz-debug", action="store_true", default=False,
25+
help=("Enable python-bugzilla debug output. This may break "
26+
"output comparison tests."))
27+
28+
29+
def pytest_ignore_collect(path, config):
30+
if ((os.path.basename(str(path)) == "test_ro_functional.py") and
31+
not config.getoption("--ro-functional")):
32+
return True
33+
34+
if ((os.path.basename(str(path)) == "test_rw_functional.py") and
35+
not config.getoption("--rw-functional")):
36+
return True
37+
38+
39+
def pytest_configure(config):
40+
if config.getoption("--redhat-url"):
41+
tests.CLICONFIG.REDHAT_URL = config.getoption("--redhat-url")
42+
if config.getoption("--pybz-debug"):
43+
logging.getLogger(bugzilla.__name__).setLevel(logging.DEBUG)
44+
os.environ["__BUGZILLA_UNITTEST_DEBUG"] = "1"
File renamed without changes.

0 commit comments

Comments
 (0)