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

Testing Python Code with pytest - Weekly Python...

Testing Python Code with pytest - Weekly Python Chat April 2017

Raphael Pierzina

April 22, 2017
Tweet

More Decks by Raphael Pierzina

Other Decks in Programming

Transcript

  1. pytest • mature testing framework for Python • available on

    OS X, Linux and Windows • compatible with CPython 2.6, 2.7, 3.3, 3.4, 3.5, 3.6 and PyPy
  2. pytest • distributed under the terms of the MIT license

    • free and open source software • developed by a thriving community of volunteers
  3. pytest • plain assert statements • regular Python comparisons •

    requires little to no boilerplate • easy parametrization
  4. Example: Test a CLI app • setup CLI runner using

    the ‘click’ library + teardown • three commands: config, update, search • two flags: --verbose, -v • smoke test (exit code is expected to be 0)
  5. import unittest from click import testing from cibopath import cli,

    utils class TestCliCommands(unittest.TestCase): def setUp(self): self.runner = testing.CliRunner() def tearDown(self): utils.clean_up() def test_config_verbose(self): command = 'config' flags = ['--verbose'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) if __name__ == '__main__': unittest.main()
  6. import unittest from click import testing from cibopath import cli,

    utils class TestCliCommands(unittest.TestCase): def setUp(self): self.runner = testing.CliRunner() def tearDown(self): utils.clean_up() def test_config_v(self): command = 'config' flags = ['-v'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) def test_config_verbose(self): command = 'config' flags = ['--verbose'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) def test_update_v(self): command = 'update' flags = ['-v'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) def test_update_verbose(self): command = 'update' flags = ['--verbose'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) def test_search_v(self): command = 'search' flags = ['-v'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) def test_search_verbose(self): command = 'search' flags = ['--verbose'] result = self.runner.invoke(cli.main, [*flags, command]) self.assertEqual(result.exit_code, 0) if __name__ == '__main__': unittest.main()
  7. import pytest from click import testing from cibopath import cli,

    utils @pytest.yield_fixture def runner(): cli_runner = testing.CliRunner() yield cli_runner utils.clean_up() def test_cli(runner): result = runner.invoke(cli.main, ['verbose', 'config']) assert result.exit_code == 0
  8. import pytest from click import testing from cibopath import cli,

    utils @pytest.yield_fixture def runner(): cli_runner = testing.CliRunner() yield cli_runner utils.clean_up() @pytest.mark.parametrize('command', [ 'config', 'update', 'search', ]) @pytest.mark.parametrize('flags', [ ['--verbose'], ['-v'], ]) def test_cli(runner, command, flags): result = runner.invoke(cli.main, [*flags, command]) assert result.exit_code == 0
  9. pytest • extensible through plugins • customizable through hooks •

    intelligent test selection with markers • powerful fixture system
  10. import pytest @pytest.mark.parametrize( 'number, word', [ (1, '1'), (3, 'Fizz'),

    (5, 'Buzz'), (10, 'Buzz'), (15, 'FizzBuzz'), (16, '16') ] ) def test_fizzbuzz(number, word): assert fizzbuzz(number) == word
  11. $ py.test -v test_parametrize.py ============================ test session starts ============================ collected

    6 items test_parametrize.py::test_fizzbuzz[1-1] PASSED test_parametrize.py::test_fizzbuzz[3-Fizz] PASSED test_parametrize.py::test_fizzbuzz[5-Buzz] PASSED test_parametrize.py::test_fizzbuzz[10-Buzz] PASSED test_parametrize.py::test_fizzbuzz[15-FizzBuzz] PASSED test_parametrize.py::test_fizzbuzz[16-16] PASSED ========================= 6 passed in 0.01 seconds ==========================
  12. import pytest @pytest.fixture(params=[ 'apple', 'banana', 'plum', ]) def fruit(request): return

    request.param def test_is_healthy(fruit): assert is_healthy(fruit)
  13. $ pytest -v test_fruits.py ========================= test session starts ========================== collected

    3 items test_fruits.py::test_is_healthy[apple] PASSED test_fruits.py::test_is_healthy[banana] PASSED test_fruits.py::test_is_healthy[plum] PASSED ======================= 3 passed in 0.01 seconds =======================
  14. def test_bake_project(cookies): """Create a project from our cookiecutter template.""" result

    = cookies.bake(extra_context={ 'repo_name': 'helloworld', }) assert result.exit_code == 0 assert result.exception is None assert result.project.basename == 'helloworld'
  15. @pytest.fixture def cookies(request, tmpdir, _cookiecutter_config_file): """Yield an instance of the

    Cookies helper class that can be used to generate a project from a template. """ template_dir = request.config.option.template output_dir = tmpdir.mkdir('cookies') output_factory = output_dir.mkdir yield Cookies( template_dir, output_factory, _cookiecutter_config_file, ) output_dir.remove()
  16. # conftest.py def pytest_collection_modifyitems(items, config): selected_items = [] deselected_items =

    [] for item in items: if 'new_fixture' in getattr(item, 'fixturenames', ()): selected_items.append(item) else: deselected_items.append(item) config.hook.pytest_deselected(items=deselected_items) items[:] = selected_items
  17. import pytest from foobar import Package, Woman, Man PACKAGES =

    [ Package('requests', 'Apache 2.0'), Package('django', 'BSD'), Package('pytest', 'MIT'), ] @pytest.fixture(params=PACKAGES) def python_package(request): return request.param @pytest.mark.parametrize('person', [ Woman('Audrey'), Woman('Brianna'), Man('Daniel'), Woman('Ola'), Man('Jameson') ]) def test_become_a_programmer(person, python_package): person.learn(python_package.name) assert person.looks_like_a_programmer def test_is_open_source(python_package): assert python_package.is_open_source
  18. test_foobar.py::test_become_a_programmer[python_package0-person0] PASSED test_foobar.py::test_become_a_programmer[python_package0-person1] PASSED test_foobar.py::test_become_a_programmer[python_package0-person2] PASSED test_foobar.py::test_become_a_programmer[python_package0-person3] PASSED test_foobar.py::test_become_a_programmer[python_package0-person4] PASSED

    test_foobar.py::test_become_a_programmer[python_package1-person0] PASSED test_foobar.py::test_become_a_programmer[python_package1-person1] PASSED test_foobar.py::test_become_a_programmer[python_package1-person2] PASSED test_foobar.py::test_become_a_programmer[python_package1-person3] PASSED test_foobar.py::test_become_a_programmer[python_package1-person4] PASSED test_foobar.py::test_become_a_programmer[python_package2-person0] PASSED test_foobar.py::test_become_a_programmer[python_package2-person1] PASSED test_foobar.py::test_become_a_programmer[python_package2-person2] PASSED test_foobar.py::test_become_a_programmer[python_package2-person3] PASSED test_foobar.py::test_become_a_programmer[python_package2-person4] PASSED test_foobar.py::test_is_open_source[python_package0] PASSED test_foobar.py::test_is_open_source[python_package1] PASSED test_foobar.py::test_is_open_source[python_package2] PASSED
  19. @pytest.fixture( params=PACKAGES, ids=operator.attrgetter('name'), ) def python_package(request): return request.param @pytest.mark.parametrize('person', [

    Woman('Audrey'), Woman('Brianna'), Man('Daniel'), Woman('Ola'), Man('Jameson') ], ids=[ 'Audrey', 'Brianna', 'Daniel', 'Ola', 'Jameson' ]) def test_become_a_programmer(person, python_package): person.learn(python_package.name) assert person.looks_like_a_programmer def test_is_open_source(python_package): assert python_package.is_open_source
  20. test_foobar.py::test_become_a_programmer[requests-Audrey] PASSED test_foobar.py::test_become_a_programmer[requests-Brianna] PASSED test_foobar.py::test_become_a_programmer[requests-Daniel] PASSED test_foobar.py::test_become_a_programmer[requests-Ola] PASSED test_foobar.py::test_become_a_programmer[requests-Jameson] PASSED

    test_foobar.py::test_become_a_programmer[django-Audrey] PASSED test_foobar.py::test_become_a_programmer[django-Brianna] PASSED test_foobar.py::test_become_a_programmer[django-Daniel] PASSED test_foobar.py::test_become_a_programmer[django-Ola] PASSED test_foobar.py::test_become_a_programmer[django-Jameson] PASSED test_foobar.py::test_become_a_programmer[pytest-Audrey] PASSED test_foobar.py::test_become_a_programmer[pytest-Brianna] PASSED test_foobar.py::test_become_a_programmer[pytest-Daniel] PASSED test_foobar.py::test_become_a_programmer[pytest-Ola] PASSED test_foobar.py::test_become_a_programmer[pytest-Jameson] PASSED test_foobar.py::test_is_open_source[requests] PASSED test_foobar.py::test_is_open_source[django] PASSED test_foobar.py::test_is_open_source[pytest] PASSED
  21. from foobar import Woman, Man, Package def pytest_make_parametrize_id(config, val): if

    isinstance(val, Woman): return u' {}'.format(val.name) elif isinstance(val, Man): return u' {}'.format(val.name) elif isinstance(val, Package): return u' {}'.format(val.name)
  22. test_foobar.py::test_become_a_programmer[ requests- Audrey] PASSED test_foobar.py::test_become_a_programmer[ requests- Brianna] PASSED test_foobar.py::test_become_a_programmer[ requests-

    Daniel] PASSED test_foobar.py::test_become_a_programmer[ requests- Ola] PASSED test_foobar.py::test_become_a_programmer[ requests- Jameson] PASSED test_foobar.py::test_become_a_programmer[ django- Audrey] PASSED test_foobar.py::test_become_a_programmer[ django- Brianna] PASSED test_foobar.py::test_become_a_programmer[ django- Daniel] PASSED test_foobar.py::test_become_a_programmer[ django- Ola] PASSED test_foobar.py::test_become_a_programmer[ django- Jameson] PASSED test_foobar.py::test_become_a_programmer[ pytest- Audrey] PASSED test_foobar.py::test_become_a_programmer[ pytest- Brianna] PASSED test_foobar.py::test_become_a_programmer[ pytest- Daniel] PASSED test_foobar.py::test_become_a_programmer[ pytest- Ola] PASSED test_foobar.py::test_become_a_programmer[ pytest- Jameson] PASSED test_foobar.py::test_is_open_source[ requests] PASSED test_foobar.py::test_is_open_source[ django] PASSED test_foobar.py::test_is_open_source[ pytest] PASSED