Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonic tests with Py.test

Pythonic tests with Py.test

Talk presented at Geek Night Recife @ ThoughtWorks

More Decks by Renato dos Santos Oliveira

Other Decks in Technology

Transcript

  1. Who?! ❏ Renato Oliveira ❏ Labcodes Software Studio ❏ Django

    Software Foundation ❏ Recently migrated to Py.test
  2. Introduction “Pytest is a mature full featured Python testing tool

    that helps you to write better programs.”
  3. Introduction - Assert with assert statement def sum(a, b): return

    a + b def test_sum(): assert sum(1, 1) == 2
  4. Migrating Unittest code Code before import unittest def to_upper(text): return

    text.upper() class TestToUpper(unittest.TestCase): def test_to_upper(self): self.assertEqual(to_upper("pug"), "PUG")
  5. Markers Gives you the ability to add some extra metadata

    to your tests. ❏ skipif ❏ xfail ❏ parametrize ❏ your owns
  6. slow test, fast test @pytest.mark.slow def test_slow(): assert 1 ==

    1 @pytest.mark.fast def test_fast(): assert 1 == 1
  7. Fixtures The purpose of test fixtures is to provide a

    fixed baseline upon which tests can reliably and repeatedly execute.
  8. @pytest.fixture() def webdriver(request): driver = Firefox() request.addfinalizer(driver.quit) return driver def

    test_pug_website_title(webdriver): webdriver.get("http://pycon.pug.pe/XXXVIII/") assert "Encontro do PUG-PE" in webdriver.title def test_python_website_title(webdriver): webdriver.get("http://python.org/") assert "Python" in webdriver.title