Skip to main content

C test-driven development framework implemented in Python

Project description

ctdd

tl:dr

C test-driven development framework implemented in Python.

Info

The testing framework is Python's native unittest, with a few extra asserts ans stuff provided by John. The tester looks for a .c and .h pair with the same name as the .py test file. It builds them into a Python extension using Crelm (which in turn uses cffi) and runs the Python tests as if it were testing Python code. C callbacks are mocked in Python (demo to follow..), meaning that there is exactly zero C code involved in the testing.

Example

There is a demo of a TDD'd add3 function in __main__.py, __main__.c and __main__.h in the root of the repo, run with python3 .. The silly filenames are to suit the Python auto-run mechanism.

The files are reproduced here (possibly out of date) with original filenames:

add3.py

from ctdd import Tester

class Add3Tests(Tester):

    def test_sut_compiles(self):
        with self.assertDoesNotRaise():
            self.sut

    def test_takes_three_f(self):
        with self.assertDoesNotRaise():
            self.sut.add3(0, 0, 0)

    def test_return_zero_for_zeros(self):
        expected = 0

        actual = self.sut.add3(0, 0, 0)

        self.assertEqual(expected, actual)

    def test_returns_sum(self):
        expected = 14

        actual = self.sut.add3(1, 4, 9)

        self.assertEqual(expected, actual)

Tester.go()

add3.h

#pragma once

int add3(int a, int b, int c);

add3.c

#include "__main__.h"

int add3(int a, int b, int c)
{
    return a + b + c;
}

To use this in real life install the package with pip install ctdd (in a virtualenv if you prefer), and run python add3.py after each add test or add code iteration.

Test output is exactly what unittest said (with distutils deprecation warning removed):

...........
----------------------------------------------------------------------
Ran 11 tests in 2.104s

OK

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

ctdd-0.0.7.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

ctdd-0.0.7-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file ctdd-0.0.7.tar.gz.

File metadata

  • Download URL: ctdd-0.0.7.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.7 Linux/5.19.0-1028-lowlatency

File hashes

Hashes for ctdd-0.0.7.tar.gz
Algorithm Hash digest
SHA256 93545ca98251075ba86287ff0dc5cf55ac580797a8f1468290893683de608b0d
MD5 ab4b4c6664a395dea4f7271ee0570393
BLAKE2b-256 8cf385cf2a306deb9cd4ee300c139921127e8ff932f957b0627fbf339ec7ac56

See more details on using hashes here.

File details

Details for the file ctdd-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: ctdd-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.7 Linux/5.19.0-1028-lowlatency

File hashes

Hashes for ctdd-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 848aa73f1f2f2deb270386a08edb7632183ac4152a1b50dc9347d0abaaf4d458
MD5 885856478e92218ef3a080829904c6ba
BLAKE2b-256 2dc37cfd922f3d43afb26409b8030660c05371bc510e192c73371f97012d7d7d

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