GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python_package/tests at main · biosustain/python_package

Expand file tree

/

tests

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

tests

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 
__init__.py
__init__.py
 
 
test_mockup.py
test_mockup.py
 
 

README.md

Outline
marp true
theme uncover
paginate true
backgroundColor

Getting started with pytesting


Quick start

Installation

Install pytest. e.g., from the "dev" dependencies

pip install ".[dev]"

How to use

To execute the tests run e.g.

pytest

Test development tips


Folder and test naming

  1. The tests for functions in <filename>.py should go in tests/test_<filename>.py

    e.g., the tests for python_package/mockup.py are in tests/test_mockup.py

  2. The test names should start with def test_<corresponding_function_name> ...

    e.g., def test_hello_world(): ...


Some Pytest decorators

  1. To indicate that the test function is expected to fail you can prepend

    @pytest.mark.xfail(raises=TypeError)
    def test_hello_world_str(): ...

  1. To setup and cleanup any resources for a test you can use pytest fixtures with yield

    @pytest.fixture
    def temp_file():
        # set up
        < code to create a file>
        # return
        yield
            the_file
        # clean up
        < code to remove the file>

Doctests

You can also include tests in your docstrings using >>> followed by the expected result e.g.

def hello_world(n):
    """
    Prints 'hello world' n-times.
        ...

    Examples
    --------
    >>> hello_world(3)
    'hello world hello world hello world'
        ...
    """

Needs addopts = --doctest-modules in pytest.ini in root of directory


Skipping in doctests

If you know that the test cannot succeed but would like to include an example usage in the docstring still then you can add # doctest: +SKIP e.g.

def saved_world(filename):
    """
    Count how many times 'hello world' is in a file.
        ...

    Examples
    --------
    >>> saved_world("not-real.txt") # doctest: +SKIP
        ...
    """
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.