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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cpytest-0.1.2-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file cpytest-0.1.2.tar.gz.

File metadata

  • Download URL: cpytest-0.1.2.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cpytest-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b3c65f887c34c9f023a74d8697c1209c41c0873dcbe1a7d6ff53d44eb78e17ef
MD5 3ac24aab817899091a947aa830ce9cf1
BLAKE2b-256 788adddf3537aaeadb397291803439300089c68850ac4ca07fd4388215ced1dc

See more details on using hashes here.

File details

Details for the file cpytest-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: cpytest-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cpytest-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d86952c8fd3ec56ea42ec18392e2d48c9fd3f242dc60ff2e4ff83028613149
MD5 4627b8921753780084fdaed089465fa1
BLAKE2b-256 0260ae58edf5cd05df323c862bed340218ca9802ae046c259fb0ce013e41a797

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page