Skip to content

1. xApp Unit Tests

When developing an xApp, you may want to ensure that your code runs as it should at every development step and does not break the links between your different modules in your system,

To guarantee a correct behaviour of your xApp and its relations, you can provide some stubs and mocks to assist the testing of your different blocks,

Stubs and mocks are predefined methods/objects that simulate the behaviour of a real methods/objects and expect a specific way of working,

This approach will help you to secure your code and protect it from breaking changes and unwanted behaviour during your development cycle,

In this context, each block should be tested independently to isolate the issues that may happen and thus demonstrate that the individual parts behave flawlessly,

Some of the different blocks that you might use when developing an xApp to interact with dRax components are already available inside of the the xApp framework,

1.1 xApp testing framework

In most cases, an xApp will use predefined interfaces to interact with the dRax components,

The xApp might want, for instance, to read values from nats and/or kafka, to set fields in redis or to assure that its configuration is correct,

For all these functionalities and much more, all the dRax interfaces are already mocked for you to simplify your tests and speedup your testing process,

Here is a non-exhaustive list of mocked components provided and pre-built inside the xapp-framework:

  • nats
  • kafka
  • redis
  • tornado
  • xapp
  • ...

Examples of using the unit tests are available in the tests folder available when creating an xapp: Development Workflows

To use the xapp framework, go in the tests folder of your deployed xapp and follow the next steps,

1.2 Setup the virtual environment

The virtual environment is used to make sure that your suite of tests works independently from your current environment,

It creates an isolated environment to protect against local packages interferences and to guarantee packages versioning installation,

Follow the steps to setup your own virtual environment and install the packages, python3.9 is required

1) Install virtualenv:

python -m pip install --user virtualenv

2) Go to your xapp tests folder

cd /your/xapp/path/root/tests

3) Create a virtualenv folder:

virtualenv -p /usr/bin/python3.9 venv_test

4) Activate the virtualenv:

source venv_test/bin/activate

5) Install needed packages for xapp and for tests:

pip install -r requirements.txt
pip install -r tests/requirements.txt

/!\ Once your virtual environment is setup and the packages installed, next time you want to run the tests you can simply reactivate your environment, no need to go over all the previous steps again /!\

1.3 Run pytest

To be able to run your tests, make sure that your virtual environment has been setup according to the previous section: Setup the virtual environment

Also, you should make sure that your virtual environment is activated,

If not, run your virtual environment:

source venv_test/bin/activate

Then, run your tests:

python -m pytest .

The previous command will run all the tests available in the folder where you run pytest,

Output example:

(venv_test) <your_prompt> python -m pytest .
====================================== test session starts =========================================
platform linux -- Python 3.9.13, pytest-7.1.0, pluggy-1.0.0
rootdir: <your_project_path>
collected 54 items

item1_test.py ..........                                                                      [ 18%]
item2_test.py ...........                                                                     [ 38%]
common_test.py .....                                                                          [ 48%]
item3_test.py ....                                                                            [ 55%]
datastore_test.py .....................                                                       [ 94%]
scenario_test.py ...                                                                          [100%]

====================================== 54 passed in 0.76s ==========================================

More info on pytest: https://docs.pytest.org/en/7.1.x/

To deactivate your virtual environment:

deactivate