skip to navigation
skip to content

unittest2pytest 0.3

Convert unittest test-cases to pytest

unittest2pytest is a tool that helps rewriting Python unittest test-cases into pytest test-cases.

In contrast to other similar tools, this unittest2pytest

  • handles keyword arguments,
  • handles single-line test-cases and several tests on one line,
  • uses context-handlers where appropriate.

This is done by using lib2to3 and Python’s mighty inspect module.


To install unittest2pytest, simply run:

pip install unittest2pytest


To print a diff of changes that unittest2pytest will make against a particular source file or directory:

unittest2pytest source_folder

To have those changes written to the files:

unittest2pytest -w source_folder

To have those changes written to another directory:

unittest2pytest -w source_folder --output-dir /some/where/else

By default, this will create backup files for each file that will be changed. You can add the -n option to not create the backups. Please do not do this if you are not using a version control system.

For more options about running particular fixers, run unittest2pytest --help or read the lib2to3 documentation. This tool is built on top of that one.


A list of the available fixers can be found with the following:

$ unittest2pytest -l
Available transformations for the -f/--fix option:

Note: if your tests use the context managers with self.assertRaises or with self.assertWarns, they will be transformed to pytest.raises or pytest.warns appropriately, but because the semantics are different, any use of the output value from the context managers (e.g. the x in with pytest.raises(ValueError) as x:) will be wrong and will require manual adjustment after the fact.


v0.3 (2016-07-26)

  • Add support for assertRaises / assertWarns context managers.
  • Add support for converting lambda arguments in assertRaises into context managers.
  • Fix some incorrect transformations.
  • Internal cleanup and fixes.

v0.2 (2015-10-20)

  • Add support for assertRegex/assertRegexpMatches, assertNotRegex, assertRaisesRegex/assertRaisesRegexp, assertWarnsRegex.
  • unittest2pytest is now a pytest subproject.
  • Minor fixes.

v0.1 (2015-10-16)

  • Initial release
File Type Py Version Uploaded on Size
unittest2pytest-0.3.tar.gz (md5) Source 2016-07-26 29KB