summaryrefslogtreecommitdiff
path: root/tests/test_parser.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-01-31 07:16:56 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-01-31 07:16:56 -0500
commit843de4ea235e7eee3ff24a39a2f8b14da9ef0db0 (patch)
tree8a4d8435595334318b5e38ef42da803e512acd4f /tests/test_parser.py
parent4fc64a97ce779c2d6bb972f0003b9b9f00e62c3a (diff)
downloadpython-coveragepy-git-843de4ea235e7eee3ff24a39a2f8b14da9ef0db0.tar.gz
refactor: unittest2pytest -w tests
One step of moving to pure pytest tests.
Diffstat (limited to 'tests/test_parser.py')
-rw-r--r--tests/test_parser.py169
1 files changed, 61 insertions, 108 deletions
diff --git a/tests/test_parser.py b/tests/test_parser.py
index 9d3f9f67..49b23f9b 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -11,6 +11,7 @@ from coverage.parser import PythonParser
from tests.coveragetest import CoverageTest, xfail
from tests.helpers import arcz_to_arcs
+import pytest
class PythonParserTest(CoverageTest):
@@ -40,9 +41,9 @@ class PythonParserTest(CoverageTest):
class Bar:
pass
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1
- })
+ }
def test_generator_exit_counts(self):
# https://github.com/nedbat/coveragepy/issues/324
@@ -53,12 +54,12 @@ class PythonParserTest(CoverageTest):
list(gen([1,2,3]))
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
1:1, # def -> list
2:2, # for -> yield; for -> exit
3:2, # yield -> for; genexp exit
5:1, # list -> exit
- })
+ }
def test_try_except(self):
parser = self.parse_source("""\
@@ -72,9 +73,9 @@ class PythonParserTest(CoverageTest):
a = 8
b = 9
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
1: 1, 2:1, 3:2, 4:1, 5:2, 6:1, 7:1, 8:1, 9:1
- })
+ }
def test_excluded_classes(self):
parser = self.parse_source("""\
@@ -86,9 +87,9 @@ class PythonParserTest(CoverageTest):
class Bar:
pass
""")
- self.assertEqual(parser.exit_counts(), {
+ assert parser.exit_counts() == {
1:0, 2:1, 3:1
- })
+ }
def test_missing_branch_to_excluded_code(self):
parser = self.parse_source("""\
@@ -98,7 +99,7 @@ class PythonParserTest(CoverageTest):
a = 4
b = 5
""")
- self.assertEqual(parser.exit_counts(), { 1:1, 2:1, 5:1 })
+ assert parser.exit_counts() == { 1:1, 2:1, 5:1 }
parser = self.parse_source("""\
def foo():
if fooey:
@@ -107,7 +108,7 @@ class PythonParserTest(CoverageTest):
a = 5
b = 6
""")
- self.assertEqual(parser.exit_counts(), { 1:1, 2:2, 3:1, 5:1, 6:1 })
+ assert parser.exit_counts() == { 1:1, 2:2, 3:1, 5:1, 6:1 }
parser = self.parse_source("""\
def foo():
if fooey:
@@ -116,14 +117,14 @@ class PythonParserTest(CoverageTest):
a = 5
b = 6
""")
- self.assertEqual(parser.exit_counts(), { 1:1, 2:1, 3:1, 6:1 })
+ assert parser.exit_counts() == { 1:1, 2:1, 3:1, 6:1 }
def test_indentation_error(self):
msg = (
"Couldn't parse '<code>' as Python source: "
"'unindent does not match any outer indentation level' at line 3"
)
- with self.assertRaisesRegex(NotPython, msg):
+ with pytest.raises(NotPython, match=msg):
_ = self.parse_source("""\
0 spaces
2
@@ -132,7 +133,7 @@ class PythonParserTest(CoverageTest):
def test_token_error(self):
msg = "Couldn't parse '<code>' as Python source: 'EOF in multi-line string' at line 1"
- with self.assertRaisesRegex(NotPython, msg):
+ with pytest.raises(NotPython, match=msg):
_ = self.parse_source("""\
'''
""")
@@ -174,8 +175,8 @@ class PythonParserTest(CoverageTest):
raw_statements = {3, 4, 5, 6, 8, 9, 10, 13, 15, 16, 17, 20, 22, 23, 25, 26}
if env.PYBEHAVIOR.trace_decorated_def:
raw_statements.update([11, 19])
- self.assertEqual(parser.raw_statements, raw_statements)
- self.assertEqual(parser.statements, {8})
+ assert parser.raw_statements == raw_statements
+ assert parser.statements == {8}
def test_class_decorator_pragmas(self):
parser = self.parse_source("""\
@@ -188,8 +189,8 @@ class PythonParserTest(CoverageTest):
def __init__(self):
self.x = 8
""")
- self.assertEqual(parser.raw_statements, {1, 2, 3, 5, 6, 7, 8})
- self.assertEqual(parser.statements, {1, 2, 3})
+ assert parser.raw_statements == {1, 2, 3, 5, 6, 7, 8}
+ assert parser.statements == {1, 2, 3}
def test_empty_decorated_function(self):
parser = self.parse_source("""\
@@ -219,9 +220,9 @@ class PythonParserTest(CoverageTest):
expected_arcs.update(set(arcz_to_arcs("-46 6-4")))
expected_exits.update({6: 1})
- self.assertEqual(expected_statements, parser.statements)
- self.assertEqual(expected_arcs, parser.arcs())
- self.assertEqual(expected_exits, parser.exit_counts())
+ assert expected_statements == parser.statements
+ assert expected_arcs == parser.arcs()
+ assert expected_exits == parser.exit_counts()
class ParserMissingArcDescriptionTest(CoverageTest):
@@ -252,31 +253,19 @@ class ParserMissingArcDescriptionTest(CoverageTest):
thing(12)
more_stuff(13)
""")
- self.assertEqual(
- parser.missing_arc_description(1, 2),
+ assert parser.missing_arc_description(1, 2) == \
"line 1 didn't jump to line 2, because the condition on line 1 was never true"
- )
- self.assertEqual(
- parser.missing_arc_description(1, 3),
+ assert parser.missing_arc_description(1, 3) == \
"line 1 didn't jump to line 3, because the condition on line 1 was never false"
- )
- self.assertEqual(
- parser.missing_arc_description(6, -5),
- "line 6 didn't return from function 'func5', "
+ assert parser.missing_arc_description(6, -5) == \
+ "line 6 didn't return from function 'func5', " \
"because the loop on line 6 didn't complete"
- )
- self.assertEqual(
- parser.missing_arc_description(6, 7),
+ assert parser.missing_arc_description(6, 7) == \
"line 6 didn't jump to line 7, because the loop on line 6 never started"
- )
- self.assertEqual(
- parser.missing_arc_description(11, 12),
+ assert parser.missing_arc_description(11, 12) == \
"line 11 didn't jump to line 12, because the condition on line 11 was never true"
- )
- self.assertEqual(
- parser.missing_arc_description(11, 13),
+ assert parser.missing_arc_description(11, 13) == \
"line 11 didn't jump to line 13, because the condition on line 11 was never false"
- )
def test_missing_arc_descriptions_for_small_callables(self):
parser = self.parse_text(u"""\
@@ -288,22 +277,14 @@ class ParserMissingArcDescriptionTest(CoverageTest):
]
x = 7
""")
- self.assertEqual(
- parser.missing_arc_description(2, -2),
+ assert parser.missing_arc_description(2, -2) == \
"line 2 didn't finish the lambda on line 2"
- )
- self.assertEqual(
- parser.missing_arc_description(3, -3),
+ assert parser.missing_arc_description(3, -3) == \
"line 3 didn't finish the generator expression on line 3"
- )
- self.assertEqual(
- parser.missing_arc_description(4, -4),
+ assert parser.missing_arc_description(4, -4) == \
"line 4 didn't finish the dictionary comprehension on line 4"
- )
- self.assertEqual(
- parser.missing_arc_description(5, -5),
+ assert parser.missing_arc_description(5, -5) == \
"line 5 didn't finish the set comprehension on line 5"
- )
def test_missing_arc_descriptions_for_exceptions(self):
parser = self.parse_text(u"""\
@@ -314,14 +295,10 @@ class ParserMissingArcDescriptionTest(CoverageTest):
except ValueError:
print("yikes")
""")
- self.assertEqual(
- parser.missing_arc_description(3, 4),
+ assert parser.missing_arc_description(3, 4) == \
"line 3 didn't jump to line 4, because the exception caught by line 3 didn't happen"
- )
- self.assertEqual(
- parser.missing_arc_description(5, 6),
+ assert parser.missing_arc_description(5, 6) == \
"line 5 didn't jump to line 6, because the exception caught by line 5 didn't happen"
- )
def test_missing_arc_descriptions_for_finally(self):
parser = self.parse_text(u"""\
@@ -346,56 +323,36 @@ class ParserMissingArcDescriptionTest(CoverageTest):
that_thing(19)
""")
if env.PYBEHAVIOR.finally_jumps_back:
- self.assertEqual(
- parser.missing_arc_description(18, 5),
+ assert parser.missing_arc_description(18, 5) == \
"line 18 didn't jump to line 5, because the break on line 5 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(5, 19),
+ assert parser.missing_arc_description(5, 19) == \
"line 5 didn't jump to line 19, because the break on line 5 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(18, 10),
+ assert parser.missing_arc_description(18, 10) == \
"line 18 didn't jump to line 10, because the continue on line 10 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(10, 2),
+ assert parser.missing_arc_description(10, 2) == \
"line 10 didn't jump to line 2, because the continue on line 10 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(18, 14),
+ assert parser.missing_arc_description(18, 14) == \
"line 18 didn't jump to line 14, because the return on line 14 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(14, -1),
- "line 14 didn't return from function 'function', "
+ assert parser.missing_arc_description(14, -1) == \
+ "line 14 didn't return from function 'function', " \
"because the return on line 14 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(18, -1),
- "line 18 didn't except from function 'function', "
+ assert parser.missing_arc_description(18, -1) == \
+ "line 18 didn't except from function 'function', " \
"because the raise on line 16 wasn't executed"
- )
else:
- self.assertEqual(
- parser.missing_arc_description(18, 19),
+ assert parser.missing_arc_description(18, 19) == \
"line 18 didn't jump to line 19, because the break on line 5 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(18, 2),
- "line 18 didn't jump to line 2, "
- "because the continue on line 10 wasn't executed"
- " or "
+ assert parser.missing_arc_description(18, 2) == \
+ "line 18 didn't jump to line 2, " \
+ "because the continue on line 10 wasn't executed" \
+ " or " \
"the continue on line 12 wasn't executed"
- )
- self.assertEqual(
- parser.missing_arc_description(18, -1),
- "line 18 didn't except from function 'function', "
- "because the raise on line 16 wasn't executed"
- " or "
- "line 18 didn't return from function 'function', "
+ assert parser.missing_arc_description(18, -1) == \
+ "line 18 didn't except from function 'function', " \
+ "because the raise on line 16 wasn't executed" \
+ " or " \
+ "line 18 didn't return from function 'function', " \
"because the return on line 14 wasn't executed"
- )
def test_missing_arc_descriptions_bug460(self):
parser = self.parse_text(u"""\
@@ -406,10 +363,8 @@ class ParserMissingArcDescriptionTest(CoverageTest):
}
x = 6
""")
- self.assertEqual(
- parser.missing_arc_description(2, -3),
- "line 3 didn't finish the lambda on line 3",
- )
+ assert parser.missing_arc_description(2, -3) == \
+ "line 3 didn't finish the lambda on line 3"
class ParserFileTest(CoverageTest):
@@ -440,18 +395,16 @@ class ParserFileTest(CoverageTest):
fname = fname + ".py"
self.make_file(fname, text, newline=newline)
parser = self.parse_file(fname)
- self.assertEqual(
- parser.exit_counts(),
- counts,
+ assert parser.exit_counts() == \
+ counts, \
"Wrong for %r" % fname
- )
def test_encoding(self):
self.make_file("encoded.py", """\
coverage = "\xe7\xf6v\xear\xe3g\xe9"
""")
parser = self.parse_file("encoded.py")
- self.assertEqual(parser.exit_counts(), {1: 1})
+ assert parser.exit_counts() == {1: 1}
def test_missing_line_ending(self):
# Test that the set of statements is the same even if a final
@@ -466,7 +419,7 @@ class ParserFileTest(CoverageTest):
""")
parser = self.parse_file("normal.py")
- self.assertEqual(parser.statements, {1})
+ assert parser.statements == {1}
self.make_file("abrupt.py", """\
out, err = subprocess.Popen(
@@ -476,7 +429,7 @@ class ParserFileTest(CoverageTest):
# Double-check that some test helper wasn't being helpful.
with open("abrupt.py") as f:
- self.assertEqual(f.read()[-1], ")")
+ assert f.read()[-1] == ")"
parser = self.parse_file("abrupt.py")
- self.assertEqual(parser.statements, {1})
+ assert parser.statements == {1}