DEV Community

ShulyAvraham
ShulyAvraham

Posted on

1

Testing for CI

Slides & Examples

Check out testing demo in Python by @szabgab

Fixture

Is the testing environment prior to running the tests

Create a test

Create a new python script with a name starting with test_

vi test_myfunc.py
Enter fullscreen mode Exit fullscreen mode

The python script should include

import mymodule # the mymodule file containing the myfunc function I want to test

def test_myfunc1():
    assert mymodule.myfunc("some input") == "some expected result"

def test_myfunc2():
    assert mymodule.myfunc("other input") == "other expected result"
Enter fullscreen mode Exit fullscreen mode

I can add more test_ functions where each function is a test case.

Run the test

pytest test_myfunc.py
Enter fullscreen mode Exit fullscreen mode

This will run all the test_ functions in test_myfunc.py and show whether the tests passed or failed, based on the expected results, per test function.

Tell pytest to run the tests in random order to make sure that the tests are independent of each other

pytest --rendom-order test_myfunc.py
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay