Skip to main content
Author:

Hartmut Goebel <h.goebel@crazy-compilers.com>

Version:
0.5.dev0
Licence:

GNU Public Licence v3 or later (GPLv3+)

Homepage:

https://github.com/pytest-dev/unittest2pytest

See Build Status on GitHub Actions

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.

Installation

To install unittest2pytest, simply run:

pip install unittest2pytest

Usage

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.

Fixes

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

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

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.

Changelog

0.5

2024-12-10

  • Convert self.fail() to pytest.fail() (#39).

  • Python >=3.9 is now required.

  • Allow non-string keys when translating assertDictContainsSubset (#54).

0.4 (2019-06-30)

  • Add support for assertDictContainsSubset.

  • Put parenthesis around expressions if required.

  • Fixed assertRaisesRegex, assertRaisesRegexp and assertWarnsRegex. The regex was getting replaced with an undefined variable pattern.

  • Fix assertRaisesRegex and assertRaisesRegexp with **kwargs and atom parameters.

  • Made assertRaisesRegex, assertRaisesRegexp and assertWarnsRegex use the match kwarg in pytest.raises instead of creating a variable with the context manager and doing an assert on re.search.

  • Add a short developer guide.

  • Remove testing on Python 3.0, 3.1, 3.2, add 3.6 and 3.7.

  • Distribute package as a universal wheel.

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

Release files for unittest2pytest 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unittest2pytest 0.5
File Size Uploaded
unittest2pytest-0.5.tar.gz 34.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unittest2pytest 0.5
File Interpreter ABI Platform
unittest2pytest-0.5-py3-none-any.whl Python 3 none any Details

Total release size: 60.3 kB

Release files / unittest2pytest-0.5.tar.gz

Download URL unittest2pytest-0.5.tar.gz
Size 34.7 kB
Tags Source
SHA-256 checksum
How to use checksums
245db168f4286470ffc932b8d055cd0062ff26dfe2c8138ba8cc93672bb520f4
BLAKE2b-256 checksum
How to use checksums
87cd8754c5c8b2962f419b40649ef32b15a68ea16f5efd92e39ca3425a1ae675
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 11, 2024.

Transparency log

Release files / unittest2pytest-0.5-py3-none-any.whl

Download URL unittest2pytest-0.5-py3-none-any.whl
Size 25.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
86d27a34e526a0667a5115738f45b0e108575dfb1f95643565aa9d867928411f
BLAKE2b-256 checksum
How to use checksums
c834b07285db118613c72aa26374058f0a5afbfd78f71f97dafb909f827edcbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 11, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

0.5 This release

2 release files

0.4

2 release files

0.3

1 release file

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page