Package: ctestgen
CtestgenRussian readme versionPython lib for generating C-programms and running them by your target-program.
Designed to assist in automated testing of programs working with C-programms.Installationpip3installctestgenExample usageCtestgenhas two main use cases: C-programs generating and
running target program with C-programs as input.C-programs generatingTo generating programs, you need to inherit abstract classctestgen.generator.TestGeneratorand reimplement method_generate_programs(), that returns c-programs abstract syntax trees.
Abstract syntax tree describing in OOP style, morehere.
Methodrun()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 codehere.
Generated codehere.fromctestgen.generatorimportTestGeneratorclassExampleTestGenerator(TestGenerator):def_generate_programs(self):generated_programs=list()foriinrange(2,6):sum_arguments=[Int('num_'+str(arg_idx))forarg_idxinrange(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)returngenerated_programsexample_generator=ExampleTestGenerator('example_generator_output')example_generator.run()This generates, as examplesum_3.c.intsum_3_nums(intnum_0,intnum_1,intnum_2){intsum=num_0+num_1+num_2;returnsum;}GeneratorAPI documentationhere.
It contains wide OOP tools to describe desired C-programs.Tests runningTo running programs, you need to inherit abstract classctestgen.runner.TestRunnerand reimplement method_on_test(), that called on every program in defined directory.TestRunnerdesigned 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.TestRunnermethodrun()describes testing pipeline, and class methods designed to override them in subclasses.Testing pipeline:recursively collect test files in defined directorycall_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 ofTestRunResultsclass, that contains run result (SUCCESS or FAIL)
and output, that may be stored in corresponding filessuccess.txtorfail.txt,
which defines by argumentdump_results_to_filesin class object creation.call_on_run_finish(tests)TestRunner collects metrics of successes and fails for every testset, and global metrics,
that stores in filemetrics.txt.In most cases it`s enough to work with abstract classctestgen.runner.BasicTestRunner,
which takes target program arguments by argumentrun_argsin 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 worderrorin target program output, test fails if it was found.
Full codehere.fromctestgen.runnerimportBasicTestRunnerclassExampleTestRunner(BasicTestRunner):def_process_program_response(self,test_dir,test_filename,program_response):if'error'inprogram_response[0]or'error'inprogram_response[1]:returnTestRunResult(TestRunResult.ResultType.FAIL,program_response[0]+program_response[1])returnTestRunResult(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()LicenseThis project is licensed under the MIT License - see theLICENSEfile for details.