def test_assert_true_with_message_formatted(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertTrue( 'one', msg='The thing why it was skipped is {}'.format( "Blah!" ) ) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert 'one', 'The thing why it was skipped is {}'.format( "Blah!" ) """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_raises_with_message(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): with self.assertRaises(ZeroDivision, msg='Blah!'): 0/1 with self.assertRaises(ZeroDivision, msg='Blah!') as excinfo: 0/1 self.assertRaises(CommandExecutionError, self._kernelpkg.remove, release=1, msg='Blah!') """) expected_code = textwrap.dedent(""" from unittest import TestCase import pytest class TestMe(TestCase): def test_one(self): with pytest.raises(ZeroDivision): 0/1 with pytest.raises(ZeroDivision) as excinfo: 0/1 pytest.raises(CommandExecutionError, self._kernelpkg.remove, release=1) """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_multilineequal(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): a = ''' foo ''' b = ''' bar ''' self.assertMultiLineEqual(a, b) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): a = ''' foo ''' b = ''' bar ''' assert a == b """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_not_almost_equal_with_message(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertNotAlmostEqual(1.0545, 1, msg='Blah') self.assertNotAlmostEqual(1.0545, 1, places=2, msg='Blah') self.assertNotAlmostEqual(1.0545, 1, delta=1, msg='Blah') self.assertNotAlmostEqual(1.0545, 1, places=2, delta=1, msg='Blah') """) expected_code = textwrap.dedent(""" from unittest import TestCase import pytest class TestMe(TestCase): def test_one(self): assert 1.0545 != pytest.approx(1, abs=1e-7), 'Blah' assert 1.0545 != pytest.approx(1, abs=1e-2), 'Blah' assert 1.0545 != pytest.approx(1, abs=1.0), 'Blah' assert 1.0545 != pytest.approx(1, abs=1.0), 'Blah' """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_raises_regex_with_message_with_statement(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): with self.assertRaisesRegex(ZeroDivision, "division by zero", msg='Blah!'): 1/0 with self.assertRaisesRegex(ZeroDivision, "division by zero", msg='Blah!') as excinfo: 1/0 """) expected_code = textwrap.dedent(""" from unittest import TestCase import pytest class TestMe(TestCase): def test_one(self): with pytest.raises(ZeroDivision, match="division by zero"): 1/0 with pytest.raises(ZeroDivision, match="division by zero") as excinfo: 1/0 """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_regex(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): pattern = "^foo" match = "foo" self.assertRegex(match, pattern) """) expected_code = textwrap.dedent(""" from unittest import TestCase import re class TestMe(TestCase): def test_one(self): pattern = "^foo" match = "foo" assert re.search(pattern, match) """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): pattern = "^foo" match = "foo" self.assertRegex(match, pattern, msg="blah") """) expected_code = textwrap.dedent(""" from unittest import TestCase import re class TestMe(TestCase): def test_one(self): pattern = "^foo" match = "foo" assert re.search(pattern, match), "blah" """) fpath = tempfiles.makepyfile(code, prefix="test_msg_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_less_equal_with_message(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertLessEqual(1, 1, msg='Blah') """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert 1 <= 1, 'Blah' """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_is_instance_with_message(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertIsInstance(some_class, object, msg='Blah') """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert isinstance(some_class, object), 'Blah' """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_is_not_none_with_message(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertIsNotNone(False, msg='Blah') """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert False is not None, 'Blah' """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_greater_equal(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertGreaterEqual(1, 1) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert 1 >= 1 """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_setqual(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertSetEqual({'a', 1}, {'b', 2}) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert {'a', 1} == {'b', 2} """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_fail_if(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.failIf(False) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert not False """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_not_in(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertNotIn(True, [False]) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert True not in [False] """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_tupleequal(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertTupleEqual(('a', 1), ('b', 2)) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert ('a', 1) == ('b', 2) """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_sequanceequal(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertSequenceEqual(['a', 1], ['b', 2]) """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert ['a', 1] == ['b', 2] """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_raises_regex_with_message_call(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertRaisesRegex(CommandExecutionError, self._kernelpkg.remove, release=1, match="error match") """) expected_code = textwrap.dedent(""" from unittest import TestCase import pytest class TestMe(TestCase): def test_one(self): pytest.raises(CommandExecutionError, self._kernelpkg.remove, release=1, match="error match") """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code
def test_assert_true(tempfiles): code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): self.assertTrue('one') self.assert_('one') """) expected_code = textwrap.dedent(""" from unittest import TestCase class TestMe(TestCase): def test_one(self): assert 'one' assert 'one' """) fpath = tempfiles.makepyfile(code, prefix="test_") fix_asserts.rewrite(fpath) with open(fpath) as rfh: new_code = rfh.read() assert new_code == expected_code