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

How to Write Pytest Plugins

How to Write Pytest Plugins

PyOhio 2019
30-minute Talk - Saturday, July 27 at 12:00pm in Cartoon 2

Pytest is a widely-used, full-featured Python testing tool that helps you write better programs. 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.

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.

Darlene Wong

July 27, 2019
Tweet

More Decks by Darlene Wong

Other Decks in Technology

Transcript

  1. How to Write Pytest Plugins PyOhio 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/or after the test functions • 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):
  6. PYTEST-TESTPLAN LOCAL PLUGIN (1/2) 1. Implement pytest_addoption in conftest.py 2.

    Implement pytest_collection_modifyitems in conftest.py 7 | © 2019 Palo Alto Networks, Inc. All Rights Reserved. Goal: Generate CSV containing test names, descriptions, markers.
  7. PYTEST-TESTPLAN LOCAL PLUGIN (2/2) 8 | © 2019 Palo Alto

    Networks, Inc. All Rights Reserved.
  8. DEMO CODE 9 | © 2019 Palo Alto Networks, Inc.

    All Rights Reserved. test_foo.py pytest.ini
  9. 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 . 11 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.
  10. 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 12 | © 2019 Palo Alto Networks, Inc. All Rights Reserved. Goal: Generate HTML test report. Upload it to Google Cloud Storage bucket.
  11. PYTEST-GCS AS AN INSTALLABLE PLUGIN 18 | © 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 .
  12. CONCLUSION • Elegance of plugin versus alternate solution • Process

    of plugin creation • Plugins interact with each other 19 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.
  13. 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 20 | © 2019 Palo Alto Networks, Inc. All Rights Reserved.