Skip to content

Commit

Permalink
Remove unnecessary unittest (#9596)
Browse files Browse the repository at this point in the history
Now that we're using pytest more aggressively, I think we should start transitioning our tests to that style rather than continuing to use unittest. This PR removes some unnecessary uses of unittest I found.

I kept the test classes (while removing the inheritance from unittest.TestCase) where I felt like it added structure or logical grouping of tests.

I verified that pytest still finds all the tests in both this branch and master by running commands like:
```
pytest $(git diff --name-only master | grep -v windows_installer_integration_tests)
```
  • Loading branch information
bmw committed Mar 2, 2023
1 parent cd467f2 commit da01846
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 353 deletions.
87 changes: 42 additions & 45 deletions acme/tests/jose_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,48 @@
import pytest


class JoseTest(unittest.TestCase):
"""Tests for acme.jose shim."""

def _test_it(self, submodule, attribute):
if submodule:
acme_jose_path = 'acme.jose.' + submodule
josepy_path = 'josepy.' + submodule
else:
acme_jose_path = 'acme.jose'
josepy_path = 'josepy'
acme_jose_mod = importlib.import_module(acme_jose_path)
josepy_mod = importlib.import_module(josepy_path)

assert acme_jose_mod is josepy_mod
assert getattr(acme_jose_mod, attribute) is getattr(josepy_mod, attribute)

# We use the imports below with eval, but pylint doesn't
# understand that.
import josepy # pylint: disable=unused-import

import acme # pylint: disable=unused-import
acme_jose_mod = eval(acme_jose_path) # pylint: disable=eval-used
josepy_mod = eval(josepy_path) # pylint: disable=eval-used
assert acme_jose_mod is josepy_mod
assert getattr(acme_jose_mod, attribute) is getattr(josepy_mod, attribute)

def test_top_level(self):
self._test_it('', 'RS512')

def test_submodules(self):
# This test ensures that the modules in josepy that were
# available at the time it was moved into its own package are
# available under acme.jose. Backwards compatibility with new
# modules or testing code is not maintained.
mods_and_attrs = [('b64', 'b64decode',),
('errors', 'Error',),
('interfaces', 'JSONDeSerializable',),
('json_util', 'Field',),
('jwa', 'HS256',),
('jwk', 'JWK',),
('jws', 'JWS',),
('util', 'ImmutableMap',),]

for mod, attr in mods_and_attrs:
self._test_it(mod, attr)
def _test_it(submodule, attribute):
if submodule:
acme_jose_path = 'acme.jose.' + submodule
josepy_path = 'josepy.' + submodule
else:
acme_jose_path = 'acme.jose'
josepy_path = 'josepy'
acme_jose_mod = importlib.import_module(acme_jose_path)
josepy_mod = importlib.import_module(josepy_path)

assert acme_jose_mod is josepy_mod
assert getattr(acme_jose_mod, attribute) is getattr(josepy_mod, attribute)

# We use the imports below with eval, but pylint doesn't
# understand that.
import josepy # pylint: disable=unused-import

import acme # pylint: disable=unused-import
acme_jose_mod = eval(acme_jose_path) # pylint: disable=eval-used
josepy_mod = eval(josepy_path) # pylint: disable=eval-used
assert acme_jose_mod is josepy_mod
assert getattr(acme_jose_mod, attribute) is getattr(josepy_mod, attribute)

def test_top_level():
_test_it('', 'RS512')

def test_submodules():
# This test ensures that the modules in josepy that were
# available at the time it was moved into its own package are
# available under acme.jose. Backwards compatibility with new
# modules or testing code is not maintained.
mods_and_attrs = [('b64', 'b64decode',),
('errors', 'Error',),
('interfaces', 'JSONDeSerializable',),
('json_util', 'Field',),
('jwa', 'HS256',),
('jwk', 'JWK',),
('jws', 'JWS',),
('util', 'ImmutableMap',),]

for mod, attr in mods_and_attrs:
_test_it(mod, attr)


if __name__ == '__main__':
Expand Down
13 changes: 5 additions & 8 deletions acme/tests/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import pytest


class MapKeysTest(unittest.TestCase):
"""Tests for acme.util.map_keys."""

def test_it(self):
from acme.util import map_keys
assert {'a': 'b', 'c': 'd'} == \
map_keys({'a': 'b', 'c': 'd'}, lambda key: key)
assert {2: 2, 4: 4} == map_keys({1: 2, 3: 4}, lambda x: x + 1)
def test_it():
from acme.util import map_keys
assert {'a': 'b', 'c': 'd'} == \
map_keys({'a': 'b', 'c': 'd'}, lambda key: key)
assert {2: 2, 4: 4} == map_keys({1: 2, 3: 4}, lambda x: x + 1)


if __name__ == '__main__':
Expand Down
59 changes: 27 additions & 32 deletions certbot-apache/tests/entrypoint_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test for certbot_apache._internal.entrypoint for override class resolution"""
import sys
import unittest
from unittest import mock

import pytest
Expand All @@ -9,38 +8,34 @@
from certbot_apache._internal import entrypoint


class EntryPointTest(unittest.TestCase):
"""Entrypoint tests"""

def test_get_configurator(self):

with mock.patch("certbot.util.get_os_info") as mock_info:
for distro in entrypoint.OVERRIDE_CLASSES:
return_value = (distro, "whatever")
if distro == 'fedora_old':
return_value = ('fedora', '28')
elif distro == 'fedora':
return_value = ('fedora', '29')
mock_info.return_value = return_value
assert entrypoint.get_configurator() == \
entrypoint.OVERRIDE_CLASSES[distro]

def test_nonexistent_like(self):
with mock.patch("certbot.util.get_os_info") as mock_info:
mock_info.return_value = ("nonexistent", "irrelevant")
with mock.patch("certbot.util.get_systemd_os_like") as mock_like:
for like in entrypoint.OVERRIDE_CLASSES:
mock_like.return_value = [like]
assert entrypoint.get_configurator() == \
entrypoint.OVERRIDE_CLASSES[like]

def test_nonexistent_generic(self):
with mock.patch("certbot.util.get_os_info") as mock_info:
mock_info.return_value = ("nonexistent", "irrelevant")
with mock.patch("certbot.util.get_systemd_os_like") as mock_like:
mock_like.return_value = ["unknown"]
def test_get_configurator():
with mock.patch("certbot.util.get_os_info") as mock_info:
for distro in entrypoint.OVERRIDE_CLASSES:
return_value = (distro, "whatever")
if distro == 'fedora_old':
return_value = ('fedora', '28')
elif distro == 'fedora':
return_value = ('fedora', '29')
mock_info.return_value = return_value
assert entrypoint.get_configurator() == \
entrypoint.OVERRIDE_CLASSES[distro]

def test_nonexistent_like():
with mock.patch("certbot.util.get_os_info") as mock_info:
mock_info.return_value = ("nonexistent", "irrelevant")
with mock.patch("certbot.util.get_systemd_os_like") as mock_like:
for like in entrypoint.OVERRIDE_CLASSES:
mock_like.return_value = [like]
assert entrypoint.get_configurator() == \
configurator.ApacheConfigurator
entrypoint.OVERRIDE_CLASSES[like]

def test_nonexistent_generic():
with mock.patch("certbot.util.get_os_info") as mock_info:
mock_info.return_value = ("nonexistent", "irrelevant")
with mock.patch("certbot.util.get_systemd_os_like") as mock_like:
mock_like.return_value = ["unknown"]
assert entrypoint.get_configurator() == \
configurator.ApacheConfigurator


if __name__ == "__main__":
Expand Down
39 changes: 18 additions & 21 deletions certbot-apache/tests/parsernode_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" Tests for ParserNode interface """

import sys
import unittest

import pytest

Expand Down Expand Up @@ -104,27 +103,25 @@ def unsaved_files(self): # pragma: no cover
interfaces.DirectiveNode.register(DummyDirectiveNode)
interfaces.BlockNode.register(DummyBlockNode)

class ParserNodeTest(unittest.TestCase):
def test_dummy():
"""Dummy placeholder test case for ParserNode interfaces"""

def test_dummy(self):
dummyblock = DummyBlockNode(
name="None",
parameters=(),
ancestor=None,
dirty=False,
filepath="/some/random/path"
)
dummydirective = DummyDirectiveNode(
name="Name",
ancestor=None,
filepath="/another/path"
)
dummycomment = DummyCommentNode(
comment="Comment",
ancestor=dummyblock,
filepath="/some/file"
)
dummyblock = DummyBlockNode(
name="None",
parameters=(),
ancestor=None,
dirty=False,
filepath="/some/random/path"
)
dummydirective = DummyDirectiveNode(
name="Name",
ancestor=None,
filepath="/another/path"
)
dummycomment = DummyCommentNode(
comment="Comment",
ancestor=dummyblock,
filepath="/some/file"
)


if __name__ == "__main__":
Expand Down

0 comments on commit da01846

Please sign in to comment.