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

How to Write Pytest Plugins

How to Write Pytest Plugins

August 17, 2019, 4:40pm-5:10pm, Boardroom

Pytest is a widely-used, full-featured Python testing tool that helps you write better programs. Whether you have been using Pytest for years or are just getting started, you may find features of Pytest that you would like to modify or customize for your own environment or specific use cases. Did you know that you can easily enhance and customize Pytest through the use of plugins? In this talk, you will learn all about some of the useful Pytest plugins that are available, and learn how to create your own plugins. We will walk through the plugin creation process by creating a plugin to upload Pytest reports to a Google Cloud Storage bucket.

Avatar for Darlene Wong

Darlene Wong

August 17, 2019
Tweet

More Decks by Darlene Wong

Other Decks in Programming

Transcript

  1. How to Write Pytest Plugins PyBay 2019 Darlene Wong Sr.

    Software Engineer, Tools & Automation at Palo Alto Networks
  2. • Customize • Test ordering (pytest-ordering) • Progress bar (pytest-sugar)

    • Reports • Extend and Integrate • Coverage (pytest-cov) • Slack (pytest-slack) • Google Cloud Storage WHY PYTEST PLUGINS? 3 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.
  3. EVOLUTION OF A PYTEST PLUGIN 4 | © 2019 Palo

    Alto Networks, Inc. All Rights Reserved. • Fixtures and Hooks • Installable Plugins • Distributed Plugins
  4. FIXTURES 5 | © 2019 Palo Alto Networks, Inc. All

    Rights Reserved. • Functions that pytest runs before and after tests • Get ready for the test • Clean up after the test • Examples • Retrieve data • Load and parse config files • Get data from database • Set up a resource • Create and delete database tables • Reusable • Scope • Put fixtures in top-level conftest.py for sharing ⇒ local plugin!
  5. HOOKS • Places in pytest code that allow you to

    change how pytest works 6 | © 2019 Palo Alto Networks, Inc. All Rights Reserved. • Define a hook function in top-level conftest.py file ⇒ local plugin! • Example pytest hooks • def pytest_addoption(parser) • def pytest_configure(config): • def pytest_collection_modifyitems(session, config, items): • def pytest_sessionfinish(session, exitstatus): pytest_addoption pytest_configure pytest_collection_modifyitems pytest_sessionfinish execute tests
  6. 7 | © 2019 Palo Alto Networks, Inc. All Rights

    Reserved. $ find . "*.py" \ | xargs grep "def test" \ | awk...
  7. 8 | © 2019 Palo Alto Networks, Inc. All Rights

    Reserved. • Finds all the tests • Knows about test markers • Access to docstrings
  8. PYTEST-TESTPLAN LOCAL PLUGIN 1. Implement pytest_addoption in conftest.py 2. Implement

    pytest_collection_modifyitems in conftest.py 9 | © 2019 Palo Alto Networks, Inc. All Rights Reserved. Goal: Generate CSV containing test names, descriptions, markers. pytest_addoption pytest_configure pytest_collection_modifyitems pytest_sessionfinish execute tests
  9. PYTEST-TESTPLAN LOCAL PLUGIN 12 | © 2019 Palo Alto Networks,

    Inc. All Rights Reserved. test_foo.py pytest.ini
  10. 14 | © 2019 Palo Alto Networks, Inc. All Rights

    Reserved. pytest --html=report.html send_report.py report.html
  11. PYTEST-GCS LOCAL PLUGIN 0. Install pytest-html plugin 1. Implement pytest_addoption

    in conftest.py 2. Implement pytest_configure in conftest.py to coordinate options with pytest-html 3. Implement pytest_sessionfinish to copy HTML report to Google Cloud Storage 15 | © 2019 Palo Alto Networks, Inc. All Rights Reserved. Goal: Generate HTML test report. Upload it to Google Cloud Storage bucket. pytest_addoption pytest_configure pytest_collection_modifyitems pytest_sessionfinish execute tests
  12. PYTEST-TESTPLAN INSTALLABLE PLUGIN 1. Create a new repository, e.g, pytest-testplan

    2. Copy relevant sections of conftest.py into new module, e.g., pytest_testplan.py 3. Create setup.py 4. pip install . 21 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.
  13. PYTEST-GCS INSTALLABLE PLUGIN 22 | © 2019 Palo Alto Networks,

    Inc. All Rights Reserved. 1. Create a new repository, e.g, pytest-gcs 2. Copy relevant sections of conftest.py into new module, e.g., pytest_gcs.py 3. Create setup.py 4. Copy supporting package to the repository, e.g., gcp 5. pip install .
  14. WHAT WE’VE LEARNED • Elegance of plugin versus alternate solution

    • Process of plugin creation • Plugins interact with each other 23 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.
  15. REFERENCES • https://docs.pytest.org • Available hooks: http://doc.pytest.org/en/latest/reference.html#hooks • Python Testing

    with pytest by Brian Okken • pytest-html plugin: https://github.com/pytest-dev/pytest-html 24 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.