Directory structure when writing tests with Python 3 standard unittest

I've heard that pytest is often used when writing tests in Python 3, but for the time being, it's standard unittest. I wondered if I should write it at //docs.python.jp/3/library/unittest.html#module-unittest), so I summarized it by trial and error.

Directory structure and code example

As far as the structure of some packages is seen, it seems that the theory is to create a test directory in the same hierarchy as the package directory.

$ tree
.
├── foo
│   ├── foo.py
│   ├──   :
│   └── xxx.py
└── tests
    ├── test_foo.py
    ├──   :
    └── test_xxx.py

foo/foo.py


class Foo:

    def say(self):
        return 'foo'

tests/test_foo.py


from unittest import TestCase
from foo.foo import Foo

class TestFoo(TestCase):

    def test_say(self):
        self.assertEqual(Foo().say(), 'foo')

Test run

Run all test cases in the tests directory

$ python -m unittest discover tests

In unittest, you can execute all the tests in the directory by using discover subcommand.

By default, all test * .py files in the specified directory are run, but you can optionally change them.

It doesn't seem to recursively follow subdirectories.

Run only one test case

$ python -m unittest tests.test_foo

Note that you can't run it directly like $ python tests / test_foo.py. Python treats the executed file directory (/ tests) as a top-level hierarchy, so you can't go back up and import packaged files, resulting in an error.

Recommended Posts

Directory structure when writing tests with Python 3 standard unittest
When writing tests with python unittests, use doCleanups for setUps that can fail
How to run tests in bulk with Python unittest
Python standard unittest usage notes
Error when playing with python
Create a directory with python
Matrix representation with Python standard input
Exclude specified cases with Python unittest
Comply with Python coding standard PEP8
Reading and writing NetCDF with Python
Reading and writing CSV with Python
RPC completed with standard Python3 modules
When matplotlib doesn't work with python2.7
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
When writing a program in Python
[Python] Format when to_csv with pandas
What should I do with the Python directory structure after all?
Addressed "Python .h: No such file or directory" when installing uWSGI with Python 3.8
Tips (control structure) that you should know when programming competitive programming with Python2
Tips (data structure) that you should know when programming competitive programming with Python2
Calculate and display standard weight with python
Snippet when searching all bits with python
Note when creating an environment with python
Precautions when solving DP problems with Python
Reading and writing JSON files with Python
UnicodeEncodeError struggle with standard output of python3
Use selenium phantomjs webdriver with python unittest
Algorithm learned with Python 11th: Tree structure
[Tips] Easy-to-read writing when connecting functions in Python
Error when installing a module with Python pip
Recommended environment and usage when developing with Python
Personal tips when doing various things with Python 3
Precautions when dealing with control structures in Python 2.6
Investigation when import cannot be done with python
I want to use Temporary Directory with Python2
Character encoding when dealing with files in Python 3
[python] [vscode] When you get angry with space-tab-mixed
Split files when writing vim plugin in python
Archive and compress the entire directory with python
Reading and writing fits files with Python (memo)
Read wav files with only Python standard packages
[Web development with Python] Precautions when saving cookies
Example of reading and writing CSV with Python
What are you using when testing with Python?
[Python] Customize Colormap when drawing graphs with matplotlib
Writing notes when running a normal python executable (file containing argparse) with Jupyter notebook