🐙 userver has built-in support for functional service tests using Yandex.Taxi Testsuite. Testsuite is based on pytest and allows developers to test their services in isolated environment. It starts service binary with minimal database and all external services mocked then allows developer to call service handlers and test their result.
Supported features:
With userver_testsuite_add() function you can easily add testsuite support to your project. Its main purpose is:
Then create testsuite target:
Some of the most useful arguments for PYTEST_ARGS:
| Argument | Description |
|---|---|
| -v | Increase verbosity |
| -s | Do not intercept stdout and stderr |
| -x | Stop on first error |
| -k EXPRESSION | Filter tests by expression |
| --service-logs-pretty | Enable logs coloring |
| --service-logs-pretty-disable | Disable logs coloring |
| --service-log-level=LEVEL | Set the log level for the service. Possible values: trace, debug, info, warning, error, critical |
| --service-wait | With this argument the testsuite will wait for the service start by user. For example under gdb. Testsuite outputs a hint on starting the service |
| -rf | Show a summary of failed tests |
userver_testsuite_add_simple() is a version of userver_testsuite_add() that makes some assumptions of the project structure. It should be invoked from the service's CMakeLists.txt as follows:
It supports the following file structure (and a few others):
You may want to create new virtual environment with its own set of packages. Or reuse existing one. That could be done this way:
Basic requirements.txt file may look like this:
Passing REQUIREMENTS is the recommended way to add Python dependencies. The venv is created at the top of the build directory and is shared by every testsuite that asks for the same requirements:
${CMAKE_BINARY_DIR}/venv-userver-testsuite-<key>
where <key> is derived from the requirements files. Suites asking for different requirements get different venvs; suites asking for the same ones share, so a project that builds many services in a single CMake project installs those dependencies once rather than once per suite.
userver_testsuite_add() registers a ctest target with name testsuite-${SERVICE_TARGET}. Run all project tests with ctest command or use filters to run specific tests:
To run pytest directly userver_testsuite_add() creates a testsuite runner script that could be found in corresponding binary directory. This may be useful to run a single testcase, to start the testsuite with gdb or to start the testsuite with extra pytest arguments:
${CMAKE_CURRENT_BINARY_DIR}/runtests-${SERVICE_TARGET}
You can use it to manually start testsuite with extra pytest arguments, e.g.:
Please refer to testuite and pytest documentation for available options. Run it with --help argument to see the short options description.
To debug the functional test you can start testsuite with extra pytest arguments, e.g.:
At the beginning of the execution the console will display the command to start the service, e.g.:
Now you can open a new terminal window and run this command in it, or if you use an IDE you can find the corresponding CMake target and add arg --config /.../config.yaml. After that it will be possible to set breakpoints and start target with debug.
By default pytest_userver plugin is included in python path. It provides basic testsuite support for userver service. To use it add it to your pytest_plugins in root conftest.py:
It requires extra PYTEST_ARGS to be passed:
The plugins match the userver cmake targets. For example, if the service links with userver::core its tests should use the pytest_userver.plugins.core plugin.
| CMake target | Matching plugin for testsuite |
|---|---|
| userver::core | pytest_userver.plugins.core |
| userver::grpc | pytest_userver.plugins.grpc |
| userver::postgresql | pytest_userver.plugins.postgresql |
| userver::clickhouse | pytest_userver.plugins.clickhouse |
| userver::redis | pytest_userver.plugins.redis |
| userver::mongo | pytest_userver.plugins.mongo |
| userver::rabbitmq | pytest_userver.plugins.rabbitmq |
| userver::kafka | pytest_userver.plugins.kafka |
| userver::mysql | pytest_userver.plugins.mysql |
| userver::ydb | pytest_userver.plugins.ydb |
Userver has built-in support for testsuite.
In order to use it you need to register corresponding components:
Headers:
Add components to components list:
Add testsuite components to config.yaml:
The essential parts of the testsuite are service_client and pytest_userver.plugins.service_client.monitor_client fixtures that give you access to the pytest_userver.client.Client and pytest_userver.client.ClientMonitor respectively. Those types allow you to interact with a running service.
Testsuite functions reference could be found at Testsuite Python support.
pytest_userver modifies static configs config.yaml and config_vars.yaml passed to pytest before starting the userver based service.
To apply additional modifications, mark a fixture with pytest_userver.config.patch. Overrides inherit this marker without repeating the decorator. See Fixture markers for marker inheritance, discovery, and ordering guarantees, and for defining custom metadata.
Example usage:
Fixture service_client is used to access the service being tested:
When tests-control component is enabled service_client is pytest_userver.client.Client instance. Which supports special testsuite related methods.
On first call to service_client service state is implicitly updated, e.g.: caches, mocked time, etc.
Use service_env fixture to provide extra environment variables for your service:
To make sure a fixture runs before the service starts, mark it with pytest_userver.service.dependency:
auto_client_deps already knows about the userver supported databases and clients, so usually you do not need to register those manually.
Mockserver allows you to mock external HTTP handlers. It starts its own HTTP server that receives HTTP traffic from the service being tested. It also allows you to install custom HTTP handlers within testsuite. In order to use it all HTTP clients must be pointed to mockserver address.
Mockserver usage example:
To connect your HTTP client to the mockserver make the HTTP client use a base URL of the form http://{mockserver}/{service_name}/.
This could be achieved by patching static config as described in service config patches and providing a mockserver address using mockserver_info.url(path):
Alternatively, use $mockserver substitution var in config_vars.testsuite.yaml:
Userver provides a way to mock internal datetime value. It only works for datetime retrieved with utils::datetime::Now(), see Mocked time sectionMocked time section for details.
From testsuite you can control it with mocked_time plugin.
Example usage:
Examples are available here:
Testpoints are used to send messages from the service to testcase and back. Typical use cases are:
First of all you should include testpoint header:
It provides TESTPOINT() and family of TESTPOINT_CALLBACK() macros that do nothing in production environment and only work when run under testsuite. Under testsuite they only make sense when corresponding testsuite handler is installed.
All testpoints has their own name that is used to call named testsuite handler. Second argument is formats::json::Value() instance that is only evaluated under testsuite.
TESTPOINT() usage sample:
Then you can use testpoint from testcase:
In order to eliminate unnecessary testpoint requests userver keeps track of testpoints that have testsuite handlers installed. Usually testpoint handlers are declared before first call to service_client which implicitly updates userver's list of testpoint. Sometimes it might be required to manually update server state. This can be achieved using service_client.update_server_state() method e.g.:
Accessing testpoint userver is not aware of will raise an exception:
Testsuite can be used to test logs written by service. To achieve this the testsuite starts a simple logs capture TCP server and tells the service to replicate logs to it on per test basis.
Example usage:
Example on logs capture usage could be found here:
Testsuite tasks facility allows you to register a custom function and call it by name from testsuite. It's useful for testing components that perform periodic job not related to its own HTTP handler.
You can use testsuite::TestsuiteTasks to register your own task:
After that you can call your task from testsuite code:
Or spawn the task asynchronously using context manager:
An example on testsuite tasks could be found here:
Use monitor_client to assert on service metrics in functional tests. See Metrics testing in testsuite for the API, examples, and portability checks. For unit tests, see Statistics and Metrics.
Testsuite provides a way to start standalone service with all mocks and database started. This can be done by adding --service-runner-mode flag to pytest, e.g.:
Please note that -s flag is required to disable console output capture.
pytest_userver provides default service runner testcase. In order to override it you have to add your own testcase with @pytest.mark.servicetest:
Testsuite allows you to create tests that start a new service instance for the test and stop it on test finish:
Such functionality slows down the tests run, but it may be required
For per-daemon fixtures see daemon_scoped_mark fixture.