diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 1 | ||||
-rw-r--r-- | tests/coveragetest.py | 8 | ||||
-rw-r--r-- | tests/helpers.py | 11 | ||||
-rw-r--r-- | tests/mixins.py | 2 | ||||
-rw-r--r-- | tests/test_api.py | 3 | ||||
-rw-r--r-- | tests/test_arcs.py | 3 | ||||
-rw-r--r-- | tests/test_backward.py | 9 | ||||
-rw-r--r-- | tests/test_context.py | 23 | ||||
-rw-r--r-- | tests/test_data.py | 13 | ||||
-rw-r--r-- | tests/test_testing.py | 20 |
10 files changed, 56 insertions, 37 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index c2e0a893..0ce494c8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,7 @@ from coverage import env # Pytest will rewrite assertions in test modules, but not elsewhere. # This tells pytest to also rewrite assertions in coveragetest.py. pytest.register_assert_rewrite("tests.coveragetest") +pytest.register_assert_rewrite("tests.helpers") # Pytest can take additional options: # $set_env.py: PYTEST_ADDOPTS - Extra arguments to pytest. diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 71f4e2c8..69dbb7cf 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -13,17 +13,17 @@ import random import re import shlex import sys +import unittest import pytest from unittest_mixins import EnvironmentAwareMixin, TempDirMixin import coverage from coverage import env -from coverage.backunittest import TestCase from coverage.backward import StringIO, import_local_file, string_class, shlex_quote from coverage.cmdline import CoverageScript -from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs +from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal from tests.helpers import run_command, SuperModuleCleaner from tests.mixins import StdStreamCapturingMixin, StopEverythingMixin @@ -40,7 +40,7 @@ class CoverageTest( StdStreamCapturingMixin, TempDirMixin, StopEverythingMixin, - TestCase, + unittest.TestCase, ): """A base class for coverage.py test cases.""" @@ -283,7 +283,7 @@ class CoverageTest( """Assert that `flist1` and `flist2` are the same set of file names.""" flist1_nice = [self.nice_file(f) for f in flist1] flist2_nice = [self.nice_file(f) for f in flist2] - self.assertCountEqual(flist1_nice, flist2_nice) + assert_count_equal(flist1_nice, flist2_nice) def assert_exists(self, fname): """Assert that `fname` is a file that exists.""" diff --git a/tests/helpers.py b/tests/helpers.py index 0621d7a9..1348aad6 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -3,6 +3,7 @@ """Helpers for coverage.py tests.""" +import collections import glob import os import re @@ -222,3 +223,13 @@ def without_module(using_module, missing_module_name): """ return mock.patch.object(using_module, missing_module_name, None) + + +def assert_count_equal(a, b): + """ + A pytest-friendly implementation of assertCountEqual. + + Assert that `a` and `b` have the same elements, but maybe in different order. + This only works for hashable elements. + """ + assert collections.Counter(list(a)) == collections.Counter(list(b)) diff --git a/tests/mixins.py b/tests/mixins.py index ab0623c0..9d096d4d 100644 --- a/tests/mixins.py +++ b/tests/mixins.py @@ -9,10 +9,10 @@ Some of these are transitional while working toward pure-pytest style. import functools import types +import unittest import pytest -from coverage.backunittest import unittest from coverage.misc import StopEverything diff --git a/tests/test_api.py b/tests/test_api.py index ea625ff1..0c1c9035 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -23,6 +23,7 @@ from coverage.files import abs_file, relative_filename from coverage.misc import CoverageException from tests.coveragetest import CoverageTest, StopEverythingMixin, TESTS_DIR, UsingModulesMixin +from tests.helpers import assert_count_equal class ApiTest(CoverageTest): @@ -43,7 +44,7 @@ class ApiTest(CoverageTest): """Assert that the files here are `files`, ignoring the usual junk.""" here = os.listdir(".") here = self.clean_files(here, ["*.pyc", "__pycache__", "*$py.class"]) - self.assertCountEqual(here, files) + assert_count_equal(here, files) def test_unexecuted_file(self): cov = coverage.Coverage() diff --git a/tests/test_arcs.py b/tests/test_arcs.py index 66777751..c6cf7952 100644 --- a/tests/test_arcs.py +++ b/tests/test_arcs.py @@ -6,6 +6,7 @@ import pytest from tests.coveragetest import CoverageTest +from tests.helpers import assert_count_equal import coverage from coverage import env @@ -1732,4 +1733,4 @@ class LineDataTest(CoverageTest): data = cov.get_data() fun1_lines = data.lines(abs_file("fun1.py")) - self.assertCountEqual(fun1_lines, [1, 2, 5]) + assert_count_equal(fun1_lines, [1, 2, 5]) diff --git a/tests/test_backward.py b/tests/test_backward.py index 767a7ac8..d750022b 100644 --- a/tests/test_backward.py +++ b/tests/test_backward.py @@ -3,16 +3,19 @@ """Tests that our version shims in backward.py are working.""" -from coverage.backunittest import TestCase +import unittest + from coverage.backward import iitems, binary_bytes, bytes_to_ints -class BackwardTest(TestCase): +from tests.helpers import assert_count_equal + +class BackwardTest(unittest.TestCase): """Tests of things from backward.py.""" def test_iitems(self): d = {'a': 1, 'b': 2, 'c': 3} items = [('a', 1), ('b', 2), ('c', 3)] - self.assertCountEqual(list(iitems(d)), items) + assert_count_equal(list(iitems(d)), items) def test_binary_bytes(self): byte_values = [0, 255, 17, 23, 42, 57] diff --git a/tests/test_context.py b/tests/test_context.py index 418849d5..20b7a290 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -12,6 +12,7 @@ from coverage.context import qualname_from_frame from coverage.data import CoverageData from tests.coveragetest import CoverageTest +from tests.helpers import assert_count_equal class StaticContextTest(CoverageTest): @@ -22,14 +23,14 @@ class StaticContextTest(CoverageTest): cov = coverage.Coverage() self.start_import_stop(cov, "main") data = cov.get_data() - self.assertCountEqual(data.measured_contexts(), [""]) + assert_count_equal(data.measured_contexts(), [""]) def test_static_context(self): self.make_file("main.py", "a = 1") cov = coverage.Coverage(context="gooey") self.start_import_stop(cov, "main") data = cov.get_data() - self.assertCountEqual(data.measured_contexts(), ["gooey"]) + assert_count_equal(data.measured_contexts(), ["gooey"]) SOURCE = """\ a = 1 @@ -67,7 +68,7 @@ class StaticContextTest(CoverageTest): assert combined.measured_contexts() == {'red', 'blue'} full_names = {os.path.basename(f): f for f in combined.measured_files()} - self.assertCountEqual(full_names, ['red.py', 'blue.py']) + assert_count_equal(full_names, ['red.py', 'blue.py']) fred = full_names['red.py'] fblue = full_names['blue.py'] @@ -92,7 +93,7 @@ class StaticContextTest(CoverageTest): assert combined.measured_contexts() == {'red', 'blue'} full_names = {os.path.basename(f): f for f in combined.measured_files()} - self.assertCountEqual(full_names, ['red.py', 'blue.py']) + assert_count_equal(full_names, ['red.py', 'blue.py']) fred = full_names['red.py'] fblue = full_names['blue.py'] @@ -157,13 +158,14 @@ class DynamicContextTest(CoverageTest): full_names = {os.path.basename(f): f for f in data.measured_files()} fname = full_names["two_tests.py"] - self.assertCountEqual( + assert_count_equal( data.measured_contexts(), - ["", "two_tests.test_one", "two_tests.test_two"]) + ["", "two_tests.test_one", "two_tests.test_two"] + ) def assert_context_lines(context, lines): data.set_query_context(context) - self.assertCountEqual(lines, data.lines(fname)) + assert_count_equal(lines, data.lines(fname)) assert_context_lines("", self.OUTER_LINES) assert_context_lines("two_tests.test_one", self.TEST_ONE_LINES) @@ -178,13 +180,14 @@ class DynamicContextTest(CoverageTest): full_names = {os.path.basename(f): f for f in data.measured_files()} fname = full_names["two_tests.py"] - self.assertCountEqual( + assert_count_equal( data.measured_contexts(), - ["stat", "stat|two_tests.test_one", "stat|two_tests.test_two"]) + ["stat", "stat|two_tests.test_one", "stat|two_tests.test_two"] + ) def assert_context_lines(context, lines): data.set_query_context(context) - self.assertCountEqual(lines, data.lines(fname)) + assert_count_equal(lines, data.lines(fname)) assert_context_lines("stat", self.OUTER_LINES) assert_context_lines("stat|two_tests.test_one", self.TEST_ONE_LINES) diff --git a/tests/test_data.py b/tests/test_data.py index 789bdd5a..fe37bd9e 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -20,6 +20,7 @@ from coverage.files import PathAliases, canonical_filename from coverage.misc import CoverageException from tests.coveragetest import CoverageTest +from tests.helpers import assert_count_equal LINES_1 = { @@ -82,23 +83,23 @@ class DataTestHelpers(CoverageTest): def assert_measured_files(self, covdata, measured): """Check that `covdata`'s measured files are `measured`.""" - self.assertCountEqual(covdata.measured_files(), measured) + assert_count_equal(covdata.measured_files(), measured) def assert_lines1_data(self, covdata): """Check that `covdata` has the data from LINES1.""" self.assert_line_counts(covdata, SUMMARY_1) self.assert_measured_files(covdata, MEASURED_FILES_1) - self.assertCountEqual(covdata.lines("a.py"), A_PY_LINES_1) + assert_count_equal(covdata.lines("a.py"), A_PY_LINES_1) assert not covdata.has_arcs() def assert_arcs3_data(self, covdata): """Check that `covdata` has the data from ARCS3.""" self.assert_line_counts(covdata, SUMMARY_3) self.assert_measured_files(covdata, MEASURED_FILES_3) - self.assertCountEqual(covdata.lines("x.py"), X_PY_LINES_3) - self.assertCountEqual(covdata.arcs("x.py"), X_PY_ARCS_3) - self.assertCountEqual(covdata.lines("y.py"), Y_PY_LINES_3) - self.assertCountEqual(covdata.arcs("y.py"), Y_PY_ARCS_3) + assert_count_equal(covdata.lines("x.py"), X_PY_LINES_3) + assert_count_equal(covdata.arcs("x.py"), X_PY_ARCS_3) + assert_count_equal(covdata.lines("y.py"), Y_PY_LINES_3) + assert_count_equal(covdata.arcs("y.py"), Y_PY_ARCS_3) assert covdata.has_arcs() diff --git a/tests/test_testing.py b/tests/test_testing.py index b6ffe1a0..67ec8dff 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -8,18 +8,18 @@ import datetime import os import re import sys +import unittest import pytest import coverage from coverage import tomlconfig -from coverage.backunittest import TestCase, unittest from coverage.files import actual_path from coverage.misc import StopEverything from tests.coveragetest import CoverageTest from tests.helpers import ( - arcs_to_arcz_repr, arcz_to_arcs, + arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal, CheckUniqueFilenames, re_lines, re_line, without_module, ) from tests.mixins import convert_skip_exceptions @@ -31,15 +31,13 @@ def test_xdist_sys_path_nuttiness_is_fixed(): assert os.environ.get('PYTHONPATH') is None -class TestingTest(TestCase): - """Tests of helper methods on `backunittest.TestCase`.""" - - def test_assert_count_equal(self): - self.assertCountEqual(set(), set()) - with pytest.raises(AssertionError): - self.assertCountEqual({1,2,3}, set()) - with pytest.raises(AssertionError): - self.assertCountEqual({1,2,3}, {4,5,6}) +def test_assert_count_equal(): + assert_count_equal(set(), set()) + assert_count_equal({"a": 1, "b": 2}, ["b", "a"]) + with pytest.raises(AssertionError): + assert_count_equal({1,2,3}, set()) + with pytest.raises(AssertionError): + assert_count_equal({1,2,3}, {4,5,6}) class CoverageTestTest(CoverageTest): |