Skip to main content

Runs tests with standard Python "unittest" module, but with modified discovery rules

Project description

neatest

Runs unit tests with standard Python unittest module.

Can be conveniently invoked from Python code as neatest.run(...) instead of running python -m unittest discover ... in shell.

Why

Testing should be simple. One-line command. A really short line.

python -m unittest discover ... is too long.

run_tests.sh is better. But not pythonic and not cross-platform.

run_tests.py is much better.

For example, if your command looks like this

$ python -m unittest discover -s ./mymodule -p '*_test.py' -t '.'

you can replace it with run_tests.py:

from neatest import run

run( pattern = "*_test.py",
     start_directory = "./mymodule" )

This script can be run with python3 run_tests.py. Now the command is short and cross-platform.

Install

pip3 install neatest

Run

Run tests with .py script

project_dir / run_tests.py

import neatest

if __name__ == "__main__":
    # all arguments are optional
    neatest.run( pattern = '*_test.py'
                 verbosity = neatest.Verbosity.quiet )

Terminal

$ python3 run_tests.py

Run tests from terminal

$ neatest

is equivalent to running the script

import neatest
neatest.run()

Arguments

Most of the arguments to the neatest.run method have the same names and meanings as the arguments of unittest and unittest discover.

Test discovery

The test discovery has different defaults than the standard unittest discover.

Filenames

neatest searches for tests in all *.py files.

So the default filename pattern is equivalent to setting -p like that:

$ python3 -m unittest discover -p "*.py"

Directories

neatest assumes, that the current directory is the top_level_directory. It is the base directory for all imports.

If the start_directory are not specified, neatest will find all the modules inside top_level_directory and will run tests for each of them.

top_level_directory
  module_a              # module_a will be tested
    __init__.py
  module_b              # module_b will be tested
    __init__.py
  module_c              # module_c will be tested
    __init__.py
    submodule           # submodule will be tested as a part of module_c 
      __init__.py         
  subdir                # subdir is not a module
      module_d          # module_d will NOT be tested 
        __init__.py     # because it is not importable     
  setup.py

So running

$ cd top_level_directory
$ neatest

is the same as running

$ cd top_level_directory
$ python3 -m unittest discover -t . -s module_a -p "*.py"
$ python3 -m unittest discover -t . -s module_b -p "*.py"
$ python3 -m unittest discover -t . -s module_c -p "*.py"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neatest-0.0.13.tar.gz (5.5 kB view hashes)

Uploaded Source

Built Distribution

neatest-0.0.13-py3-none-any.whl (7.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page