Skip to content

Instantly share code, notes, and snippets.

@borgstrom
Last active June 14, 2018 03:20
Show Gist options
  • Save borgstrom/bbe8237c2f821f8e63a40ad151ebc66d to your computer and use it in GitHub Desktop.
Save borgstrom/bbe8237c2f821f8e63a40ad151ebc66d to your computer and use it in GitHub Desktop.
Pytest assertion for unittest assertions (assertEquals, assertAlmostEqual, etc...)
import time
import pytest
from unittest import TestCase
IPSUM1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ut semper quam.'
IPSUM2 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue sagittis eros ac aliquet.'
class Test1(TestCase):
def test_old_almost(self):
self.assertAlmostEqual(0.9, 1.0)
def test_new_almost(self):
assert 0.9 == pytest.approx(1.0)
class Test2(TestCase):
def test_old_large_text(self):
self.assertEqual(IPSUM1, IPSUM2)
def test_new_large_text(self):
assert IPSUM1 == IPSUM2
======================================= test session starts ========================================
platform darwin -- Python 2.7.14, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /private/tmp/assert, inifile:
plugins: mock-1.2, cov-2.5.1
collected 4 items                                                                                   

test_assertions.py FFFF

============================================= FAILURES =============================================
______________________________________ Test1.test_new_almost _______________________________________

self = <test_assertions.Test1 testMethod=test_new_almost>

    def test_new_almost(self):
>       assert 0.9 == pytest.approx(1.0)
E       AssertionError: assert 0.9 == 1.0 +- 1.0e-06
E        +  where 1.0 +- 1.0e-06 = <function approx at 0x10497b320>(1.0)
E        +    where <function approx at 0x10497b320> = pytest.approx

test_assertions.py:16: AssertionError
______________________________________ Test1.test_old_almost _______________________________________

self = <test_assertions.Test1 testMethod=test_old_almost>

    def test_old_almost(self):
>       self.assertAlmostEqual(0.9, 1.0)
E       AssertionError: 0.9 != 1.0 within 7 places

test_assertions.py:13: AssertionError
____________________________________ Test2.test_new_large_text _____________________________________

self = <test_assertions.Test2 testMethod=test_new_large_text>

    def test_new_large_text(self):
>       assert IPSUM1 == IPSUM2
E       AssertionError: assert 'Lorem ipsum ... semper quam.' == 'Lorem ipsum d...s ac aliquet.'
E         Skipping 48 identical leading characters in diff, use -v to show
E         - ng elit. Duis ut semper quam.
E         + ng elit. Donec congue sagittis eros ac aliquet.

test_assertions.py:24: AssertionError
____________________________________ Test2.test_old_large_text _____________________________________

self = <test_assertions.Test2 testMethod=test_old_large_text>

    def test_old_large_text(self):
>       self.assertEqual(IPSUM1, IPSUM2)
E       AssertionError: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ut semper quam.' != 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue sagittis eros ac aliquet.'

test_assertions.py:21: AssertionError
===================================== 4 failed in 0.08 seconds =====================================

Which would you rather debug...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment