Automatically convert unittests to pytest
Project description
pytestify
A tool to automatically change unittest to pytest. Similar to unittest2pytest, but with a few more features and written using AST and tokenize, rather than lib2to3.
Big thanks to pyupgrade, which this is based off architecturally.
Installation
TODO
Usage
pytestify path/to/file.py
pytestify path/to/folder/
Please read over all changes that pytestify makes. It's a new package, so there are bound to be issues.
Implemented features
Test class names
Remove TestCase parent class, and make sure tests start with Test
class TestThing(unittest.TestCase): # class TestThing:
class TestThing(TestCase, ClassB): # class TestThing(ClassB):
class ThingTest(unittest.TestCase): # class TestThing:
class Thing(unittest.TestCase): # class TestThing:
Setup / teardowns
def setUp(self): # def setup_method(self):
def tearDown(self): # def teardown_method(self):
Asserts
Rewrite unittest assert methods using the assert
keyword.
# asserting one thing
self.assertTrue(a) # assert a
self.assertFalse (a) # assert not a
self.assertIsNone(a) # assert a is None
self.assertIsNotNone(a) # assert a is not None
# asserting two things
self.assertEqual(a, b) # assert a == b
self.assertNotEqual(a, b) # assert a != b
self.assertIs(a, b) # assert a is b
self.assertIsNot(a, b) # assert a is not b
self.assertIn(a, b) # assert a in b
self.assertNotIn(a, b) # assert a not in b
self.assertListEqual(a, b) # assert a == b
self.assertDictEqual(a, b) # assert a == b
self.assertSetEqual(a, b) # assert a == b
self.assertItemsEqual(a, b) # assert sorted(a) == sorted(b)
self.assertGreater(a, b) # assert a > b
self.assertLess(a, b) # assert a < b
self.assertGreaterEqual(a, b) # assert a >= b
self.assertLessEqual(a, b) # assert a <= b
self.assertRegex(a, b) # assert a.search(b)
self.assertNotRegex(a, b) # assert not a.search(b)
# error messages
self.assertTrue(a, msg='oh no!') # assert a, 'oh no!'
Note: assertCountEqual
is NOT supported here. It is tricky to implement, and pytest has voiced
hesitations about including it in
the near future. You can still use unittest's implementation.
Multi-line asserts
Since assert (a == b, 'err')
is equivalent to asserting a tuple, and thus is always True
.
self.assertEqual( # assert \
a, # a == \
b, # b
)
self.assertEqual( # assert \
a, # a == \
b, # b, \
msg='oh no!' # 'oh no!'
)
Exceptions
self.assertRaises(OSError) # pytest.raises(OSError)
self.assertWarns(OSError) # pytest.warns(OSError)
with self.assertRaises(OSError) as e: # with pytest.raises(OSError) as e
with self.assertWarns(OSError) as e: # with pytest.warns(OSError) as e
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytestify-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48bbbe75ea66b13bd9793a3d1bf859aeb02447b4ef9e695519d61c2ca9bfe0dd |
|
MD5 | 755b71224628b6c30efa5017fa42c459 |
|
BLAKE2b-256 | 403fff978d049d72ad230394bf42e42bf3cec962bb63da92617b404d81f2b19f |