Skip to main content

Test C Code using Python/pytest

Project description

CPYTEST

Pipeline Status Test Coverage PyPI - Python Version PyPI - License

CPYTEST is a python package that allows you to write unit tests for your C code. It uses cffi to generate python bindings for C code, so that you can use the full pytest framework for testing your C code.

Installation

t.b.d.

Runing the Example

Dependencies

pip install poetry pytest
sudo apt install gcovr
sudo apt install lcov

Build and Run Example Test

Using Poetry

poetry install
poetry run pip install pycparser==2.21
poetry run python3 -m cpytest example/test/cpytest.yaml
poetry run pytest example/test/test_example.py

Using virtualenv

virtualenv .venv
source .venv/bin/activate
poetry install
poetry run pip install pycparser==2.21
python3 -m cpytest example/test/cpytest.yaml
pytest example/test/test_example.py

Generate Coverage

lcov --capture --directory example/src --base-directory example/test/build --output-file coverage.info --rc lcov_branch_coverage=1 --no-external
genhtml coverage.info --output-directory coverage_report --rc genhtml_branch_coverage=1
ls coverage_report/index.html

Limitations

pycparser / pycparserext version conflict

pycparserext requires pycparser==2.20, but we need pycparser==2.21 (for #pragma support). poetry doesn't solve this conflict for us, but we can manually install pycparser=2.21 using poetry: poetry run pip install pycparser==2.21. The corresponding pip install error that is shown can be ignored.

#pragma vs. __attribute__((packed)) support

pycparser doesn't support __attribute__((packed)) on structs, but it supports #pragma pack. We need to use #pragma pack in order to the correct python code for packed structs.

__attribute__ limitations of pycparserext

pycparserext has only limited support for GCC __attribute__ extensions. To avoid problems, we run a postprocessor after the preprocessor, which filters out all __attribute__ extensions.

Development

Run QA

tox is used to run all QA tools at once.

tox

Docker

Run the Example

cd docker
docker-compose up --build test-example-project
docker-compose down

Run the interactive docker shell

docker-compose up --build -d shell
docker-compose exec shell bash
docker-compose down

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

cpytest-0.1.2.tar.gz (20.6 kB view hashes)

Uploaded Source

Built Distribution

cpytest-0.1.2-py3-none-any.whl (23.5 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