Skip to main content

Ctestgen

Russian readme version

Python lib for generating C-programms and running them by your target-program. Designed to assist in automated testing of programs working with C-programms.

Installation

pip3 install ctestgen

Example usage

Ctestgen has two main use cases: C-programs generating and running target program with C-programs as input.

C-programs generating

To generating programs, you need to inherit abstract class ctestgen.generator.TestGenerator and reimplement method _generate_programs(), that returns c-programs abstract syntax trees. Abstract syntax tree describing in OOP style, more here. Method run() calls _generate_programs() and then stores programs in output dir, which is defined when creating a class object: ExampleTestGenerator('example_generator_output').

Describe the class, that generating programs with sum function, that takes from 2 to 5 arguments. Full code here. Generated code here.

from ctestgen.generator import TestGenerator
class ExampleTestGenerator(TestGenerator):
    def _generate_programs(self):
        generated_programs = list()
        for i in range(2, 6):
            sum_arguments = [Int('num_' + str(arg_idx)) for arg_idx in range(i)]
            sum_function = Function('sum_' + str(i) + '_nums', Int, sum_arguments)
            sum_result = Int('sum')
            sum_body = CodeBlock(
                Assignment(VarDeclaration(sum_result), Add(sum_arguments)),
                Return(sum_result)
            )
            sum_function.set_body(sum_body)
            example_program = Program('sum_' + str(i))
            example_program.add_function(sum_function)
            generated_programs.append(example_program)
        return generated_programs

example_generator = ExampleTestGenerator('example_generator_output')
example_generator.run()

This generates, as example sum_3.c.

int sum_3_nums(int num_0, int num_1, int num_2) {
  int sum = num_0 + num_1 + num_2;
  return sum;
}

Generator API documentation here. It contains wide OOP tools to describe desired C-programs.

Tests running

To running programs, you need to inherit abstract class ctestgen.runner.TestRunner and reimplement method _on_test(), that called on every program in defined directory. TestRunner designed to run target program with every file, that contained in the target directory and its subdirectories and have target file extensions, which defines in class object creation, by default it`s .c.

TestRunner method run() describes testing pipeline, and class methods designed to override them in subclasses.

Testing pipeline:

  • recursively collect test files in defined directory
  • call _on_run_start(tests)
  • for every dir, that contained test files, call _on_testdir(test_dir, test_filenames)
  • for every file in that dir, call _on_test(test_dir, test_filename, env), that returns object of TestRunResults class, that contains run result (SUCCESS or FAIL) and output, that may be stored in corresponding files success.txt or fail.txt, which defines by argument dump_results_to_files in class object creation.
  • call _on_run_finish(tests)

TestRunner collects metrics of successes and fails for every testset, and global metrics, that stores in file metrics.txt.

In most cases it`s enough to work with abstract class ctestgen.runner.BasicTestRunner, which takes target program arguments by argument run_args in class object creation. In _on_test(test_dir, test_filename, env) it calls target program as subprocess, and pass results to method _process_program_response(self, test_dir, test_filename, program_response). So it`s enough to override this method in subclass.

Describe the class, that checking word error in target program output, test fails if it was found. Full code here.

from ctestgen.runner import BasicTestRunner
class ExampleTestRunner(BasicTestRunner):
    def _process_program_response(self, test_dir, test_filename, program_response):
        if 'error' in program_response[0] or 'error' in program_response[1]:
            return TestRunResult(TestRunResult.ResultType.FAIL, program_response[0] + program_response[1])
        return TestRunResult(TestRunResult.ResultType.SUCCESS, program_response[0] + program_response[1])

example_runner = ExampleTestRunner(['tsar'],
                                   output_base_dir='runner_output',
                                   test_filename_extensions=['.c'],
                                   test_base_dir='example_generator_output',
                                   runner_name='example_runner')
example_runner.run()

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for ctestgen 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ctestgen 0.1.2
File Size Uploaded
ctestgen-0.1.2.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ctestgen 0.1.2
File Interpreter ABI Platform
ctestgen-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 31.8 kB

Release files / ctestgen-0.1.2.tar.gz

Download URL ctestgen-0.1.2.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b031830ed356a8b8855f45dec8b7d43bb9d4fdfe722f552ded531044c7d7c356
BLAKE2b-256 checksum
How to use checksums
ce4db87f33e014752902b982d83e84e1aa48aa801457e126e24d57f284d4dd3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release files / ctestgen-0.1.2-py3-none-any.whl

Download URL ctestgen-0.1.2-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
534fc39137d10de648bf66a486b3c5b4c5b05a7ad058322f955c46268e002617
BLAKE2b-256 checksum
How to use checksums
b4f400ea683bf07426cb392ba3380041be4da15fd42eed744ee2ab673100488a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page