13.2.6. Testing frameworkΒΆ

NextGIS Web testing framework is based on pytest library with some additions (predefined fixtures, marks) and conventions. It supports unit and functional tests.

13.2.6.1. File layoutΒΆ

Each NextGIS Web component have its own set of tests located inside test directory. Consider the directory structure on the example for package package and component component.

πŸ—                               # Repository root directory
β”œβ”€β”€ πŸ— package                   # Package source root
β”‚   β”œβ”€β”€ πŸ— component             # Component root directory
β”‚   β”‚   β”œβ”€β”€ πŸ— test              # Test root directory
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ—€ data          # Test data directory
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ—Ž __init__.py   # Dummy __init__ module
β”‚   β”‚   β”‚   └── πŸ—Ž test_*.py     # Test module files
β”‚   β”‚   └── πŸ—Ž __init__.py       # And other component files
β”‚   └── πŸ—Ž __init__.py           # Package files and other components
└── πŸ—Ž setup.py                  # Setuptools configuration file

13.2.6.2. Unit testsΒΆ

Unit tests are designed for testing modules and usually don’t require additional configuration. Refer pytest documentation and Writing tests section.

# package/component/test/test_unit.py
from .. import some_function

def test_some_function():
    assert some_function(1) == 0

13.2.6.3. Functional testsΒΆ

Functional tests operate on data, and NextGIS Web must be configured and have the database initialized via nextgisweb initialize_db. Some tests modify data, so don’t run these tests in a production environment.

13.2.6.3.1. Server-sideΒΆ

Server-side tests are executed in the same context as other NextGIS Web console scripts. The environment can be loaded with ngw_env fixture, which initializes components and the environment.

# package/component/test/test_env.py

def test_component(ngw_env):
    ngw_env.component.some_component_method()

Tests inside a transaction can be performed using ngw_txn fixture. It begins a new transaction and rollbacks it at exit, flushing changes to the database before that.

# package/component/test/test_txn.py
from ..model import SomeModel

def test_transaction(ngw_txn):
    SomeModel(field='value').persist()  # Dummy record insert

13.2.6.3.2. Web applicationΒΆ

For testing via HTTP requests fixture ngw_webtest_app can be used. It’s represents WebTest TestApp instance which can be used for doing requests.

# package/component/test/test_webapp.py

def test_api_method(ngw_webtest_app):
    ngw_webtest_app.get('/api/component/component/method')

13.2.6.4. Writing testsΒΆ

13.2.6.4.1. Naming conventionsΒΆ

Follow pytest default naming conventions - test modules and functions must have test_ prefix.

Note

Do not forget to add a dummy __init__.py file to the test directory.

13.2.6.5. Running testsΒΆ

$ export NEXTGISWEB_CONFIG=path/to/config.ini
$ python -m pytest package                     # All tests from package
$ python -m pytest package/component           # Only tests from component
$ python -m pytest --pyargs package.component  # Same but by Python module name

Note

The last option with --pyargs might be useful when running tests in Crater / NGWDocker environment. Path-based options won’t work because of symlinks inside site-packages directory.