Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
pytest Quick Start Guide

You're reading from   pytest Quick Start Guide Write better Python code with simple and maintainable tests

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789347562
Length 160 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bruno Oliveira Bruno Oliveira
Author Profile Icon Bruno Oliveira
Bruno Oliveira
Arrow right icon
View More author details
Toc

Converting asserts with unitest2pytest

Once you have changed the test runner to pytest, you can take advantage of writing plain assert statements instead of self.assert* methods.

Converting all the method calls is boring and error-prone, that's why the unittest2pytest tool exists. It converts all self.assert* method calls to plain asserts, and also converts self.assertRaises calls to the appropriate pytest idiom.

Install it using pip:

λ pip install unittest2pytest

Once installed, you can now execute it on the files you want:

λ unittest2pytest test_simple2.py
RefactoringTool: Refactored test_simple2.py
--- test_simple2.py (original)
+++ test_simple2.py (refactored)
@@ -5,6 +5,7 @@
import unittest
from collections import namedtuple
from pathlib import Path
+import pytest

DATA = """
Main Grid,48,44
@@ -49,12 +50,12 @@
self.grids = list(iter_grids_from_csv...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime