Thursday, October 14, 2021

Test and Code: 166: unittest expectedFailure and xfail

xfail isn't just for pytest tests. Python's unittest has @unittest.expectedFailure.

In this episode, we cover:

  • using @unittest.expectedFailure
  • the results of passing and failing tests with expectedFailure
  • using pytest as a test runner for unittest
  • using pytest markers on unittest tests

Docs for expectedFailure:
https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures

Some sample code.
unittest only:

import unittest

class ExpectedFailureTestCase(unittest.TestCase):

    @unittest.expectedFailure
    def test_fail(self):
        self.assertEqual(1, 0, "broken")

    @unittest.expectedFailure
    def test_pass(self):
        self.assertEqual(1, 1, "not broken")

unittest with pytest markers:

import unittest
import pytest


class ExpectedFailureTestCase(unittest.TestCase):

    @pytest.mark.xfail
    def test_fail(self):
        self.assertEqual(1, 0, "broken")

    @pytest.mark.xfail
    def test_pass(self):
        self.assertEqual(1, 1, "not broken")

Sponsored By:

Support Test & Code

<p>xfail isn&#39;t just for pytest tests. Python&#39;s unittest has <code>@unittest.expectedFailure</code>.</p> <p>In this episode, we cover:</p> <ul> <li>using <code>@unittest.expectedFailure</code></li> <li>the results of passing and failing tests with <code>expectedFailure</code></li> <li>using pytest as a test runner for unittest</li> <li>using pytest markers on unittest tests</li> </ul> <p>Docs for <code>expectedFailure</code>: <br> <a href="https://ift.tt/3FMIk3U" rel="nofollow">https://ift.tt/3mUZmnZ> </p> <p>Some sample code. <br> unittest only:</p> <pre><code class="python">import unittest class ExpectedFailureTestCase(unittest.TestCase): @unittest.expectedFailure def test_fail(self): self.assertEqual(1, 0, &quot;broken&quot;) @unittest.expectedFailure def test_pass(self): self.assertEqual(1, 1, &quot;not broken&quot;) </code></pre> <p>unittest with pytest markers:</p> <pre><code class="python">import unittest import pytest class ExpectedFailureTestCase(unittest.TestCase): @pytest.mark.xfail def test_fail(self): self.assertEqual(1, 0, &quot;broken&quot;) @pytest.mark.xfail def test_pass(self): self.assertEqual(1, 1, &quot;not broken&quot;) </code></pre><p>Sponsored By:</p><ul><li><a href="https://ift.tt/2tzXV5e" rel="nofollow">Patreon Supporters</a>: <a href="https://ift.tt/2tzXV5e" rel="nofollow">Help support the show with as little as $1 per month and be the first to know when new episodes come out.</a></li></ul><p><a href="https://ift.tt/2tzXV5e" rel="payment">Support Test & Code</a></p>

from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...