Package: pytestify
pytestifyA tool to automatically change unittest to pytest. Similar tounittest2pytest,
but with a few more features and written using AST and tokenize, rather
than lib2to3.Big thanks topyupgrade, which
this project has learned from.Installationpip install pytestifyUsagepytestify path/to/file.pyorpytestify path/to/folder/Optional arguments--keep-method-casing--with-count-equalPlease read over all changes that pytestify makes. It's a new
package, so there are bound to be issues.Implemented featuresTest class namesRemoveTestCaseparent class, and make sure tests start withTest. We are keeping the test classes themselves, but you can remove them manually.classTestThing(unittest.TestCase):# class TestThing:classTestThing(TestCase,ClassB):# class TestThing(ClassB):classThingTest(unittest.TestCase):# class TestThing:classThing(unittest.TestCase):# class TestThing:Setup / teardownsdefsetUp(self):# def setup_method(self):deftearDown(self):# def teardown_method(self):defsetUpClass(self):# def setup_class(self):deftearDownClass(self):# def teardown_class(self):AssertsRewrite unittest assert methods using theassertkeyword.# asserting one thingself.assertTrue(a)# assert aself.assertFalse(a)# assert not aself.assertIsNone(a)# assert a is Noneself.assertIsNotNone(a)# assert a is not None# asserting two thingsself.assertEqual(a,b)# assert a == bself.assertNotEqual(a,b)# assert a != bself.assertIs(a,b)# assert a is bself.assertIsNot(a,b)# assert a is not bself.assertIn(a,b)# assert a in bself.assertNotIn(a,b)# assert a not in bself.assertListEqual(a,b)# assert a == bself.assertDictEqual(a,b)# assert a == bself.assertSetEqual(a,b)# assert a == bself.assertGreater(a,b)# assert a > bself.assertLess(a,b)# assert a < bself.assertGreaterEqual(a,b)# assert a >= bself.assertLessEqual(a,b)# assert a <= bself.assertRegex(a,b)# assert a.search(b)self.assertNotRegex(a,b)# assert not a.search(b)self.assertAlmostEqual(a,b)#   assert a == pytest.approx(b)self.assertAlmostEqual(a,b,places=2)#   assert a == pytest.approx(b, abs=0.01)self.assertAlmostEquals(a,b,delta=2)#   assert a == pytest.approx(b, abs=2)# improves the assert if reasonableself.assertEqual(a,None)# assert a is Noneself.assertEqual(a,True)# assert a is True# error messagesself.assertTrue(a,msg='oh no!')# assert a, 'oh no!'Multi-line assertsSinceassert (a == b, 'err')is equivalent to asserting a tuple, and thus is alwaysTrue.self.assertEqual(# assert a == \a,#     bb,)self.assertEqual(# assert a == \a,#     b, \b,#     'oh no!'msg='oh no!')camelCase to snake_caseDisable this behavior with--keep-method-casingdeftestThing(self):# def test_thing(self):deftestHTTPThing(self):# def test_httpthing(self):assertCountEqualTheassertCountEqualrewrite is risky, so opt-in withpytest path/to/file --with-count-equal.self.assertItemsEqual(a,b)# assert sorted(a) == sorted(b)self.assertCountEqual(a,b)# assert sorted(a) == sorted(b)Note that pytest has no version of either of these methods. Seethis threadfor more
information. You can also useunittest's implementation.Exceptionsself.assertRaises(OSError)# pytest.raises(OSError)self.assertWarns(OSError)# pytest.warns(OSError)withself.assertRaises(OSError)ase:# with pytest.raises(OSError) as ewithself.assertWarns(OSError)ase:# with pytest.warns(OSError) as eSkipping / Expecting failure# decorated@unittest.skip('some reason')# @pytest.mark.skip('some reason')@unittest.skipIf(some_bool)# @pytest.mark.skipif(some_bool)@unittest.skipUnless(some_bool)# @pytest.mark.skipif(not some_bool)@unittest.expectedFailure# @pytest.mark.xfail# not decoratedunittest.skip('some reason')# pytest.skip('some reason')unittest.skipTest('some reason')# pytest.skip('some reason')unittest.fail('some reason')# pytest.fail('some reason')