Skip to content

tusharsadhwani/astest

Repository files navigation

astest: assert based testing

The quick, easy way to write tests in Python.

Usage

Get the tool by installing it from pip:

pip install astest

The tool doesn't have to be installed in your project's environment, you can also install it in your global Python (since it has no dependencies).

Then give it a file containing "tests" (explained below):

astest test_file.py

You can also easily run it via pipx:

pipx run astest test_file.py

Test files

astest expects a file that does assert's at runtime.

So to write a test for your code, simply do asserts in your file:

from my_library import add

assert add(12, 13) == 25
assert add(33, 77) == 100  # This one is false
assert add(123, 123) == 246

Running this will look like this:

$ astest examples/simple.py
Test 1..................................................................PASSED
Test 2..................................................................FAILED
Failing test: assert add(33, 77) == 100
Test 3..................................................................PASSED
===================== 1 failed, 2 passed in 0.00 seconds =====================

Debugging

If a test fails and you wish to debug it, you can run astest with --debug:

$ astest examples/simple.py
Test 1..................................................................PASSED
Test 2..................................................................FAILED
Failing test: assert add(33, 77) == 100
Starting debug session:
>>> add(33, 77)
110
>>> # Oh okay.
>>> ^D
Test 3..................................................................PASSED
===================== 1 failed, 2 passed in 0.00 seconds =====================

FAQ

  • Why not pytest?

    For a very simple project (eg. that only exposes one function), having to define several separate test functions or using something like pytest.mark.parametrize to write a test suite seemed overkill.

    I was wondering if just "a bunch of asserts" can be turned into a test suite. Turns out you can.

  • Weird name.

    That's not a question, but yes.

  • What does the name mean / How do you pronounce it?

    It can mean a few things:

    • assert tests
    • AST test (the package heavily relies on the AST module to work while remaining fast)
    • A simple test library

    I'll leave you to decide on the pronunciation.

Local Development / Testing

  • Create and activate a virtual environment
  • Run pip install -r requirements-dev.txt to do an editable install
  • Run pytest to run tests

Type Checking

Run mypy .

Create and upload a package to PyPI

Make sure to bump the version in setup.cfg.

Then run the following commands:

rm -rf build dist
python setup.py sdist bdist_wheel

Then upload it to PyPI using twine:

twine upload dist/*

About

The quick, easy way to write tests in Python.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages