Package: contest
contestACONsole applicationTESTerIntroductioncontestis a testing application that exercises a program with configured input and then checks the output with some expected content. Simply:contestvalidates executable output given specific inputs.MotivationI wrote this for a class that I teach to help with grading. Given the configuration driven nature of it (how else should a testing framework/tool work?)contestlets me define multiple test cases for particular programs (assignments) so that not only is grading easier for me, but I can integrate assignments into a grading pipeline so that I can do as little work as possible and my students can get immediate feedback on submissions. Assignments here are usually single-file programs that are not necessarily unit-testable without adding in boilerplate and nonstandard language features. Letting students build normal programs and then verify their behavior is the goal.InstallationpipinstallcontestYou may install from this repo, clone and simply:pythonsetup.pyinstallHow It WorksOverviewYAML is the preferred choice of input forcontestfor a few reasons, but most notably for its easy to follow syntax and allowance of multiline strings (sorry JSON).contestconsumes an input file that specifies at least one executable and then whatever additional information is provided. Check out the test skeleton below to see what can be specified; the main ones though are the input and output streamsstdout,stderr, andstderras well and CLI inputsargv. You can specify newly generated files that you expect to be created and even go further and specify custom tests (asPythonfiles) that cover the things simple I/O comparisons do not. Lastly, you may also specify environment variables to be set during the execution; and you may also scrub the environment before adding custom keys. Note that scrubbing the environment may prevent your executable from running altogether! For now you are better off allowing your current environment to persist and simply overwrite what needs to be set. Environment variables specified within a test-case take precedence over what is in your current environment.Test Skeletonexecutable:# !!str, name of the executable to use for all teststest-cases:# !!seq, list of all test cases-name:# !!str, name of the testscrub-env:# !!bool, flag to remove the current environmentenv:# !!map, environment variables to setresources:# !!seq, list of resources to copy to the test directory, need to provide a src and dstexecutable:# !!str, name of the executable to use for this test case onlyargv:# !!seq, list of arguments to pass to the executablestdin:# !!str || !!seq inputs to standard input stream# * see belowreturncode:# !!int, expected return codestdout:# !!str || !!seq || !!map, expected output in standard output stream# ** see belowstderr:# !!str || !!seq || !!map, expected output in standard error stream# ** see belowofstreams:# !!seq, list of files to compare# *** see belowextra-tests:# !!seq, list of additional modules to load for additional tests* The stdin field can either be a block of text (one entry to stdin per line) or can be provided as a list.** These fields can either be text blocks, lists, or dictionaries. As a text block or list, it should be the expected output as is. As dictionaries the following fields are allowed:text:# !!str, expected output as a stringfile:# !!str, file containing the text to compare against. useful for keeping the size of these files small if desiredempty:# !!bool, flag to explicitly check if a stream is empty or nonempty. all other checks are ignoredstart:# !!int, 0-indexed line offset to specify where to start comparisonscount:# !!int, number of lines to compareAll fields are optional, so long as whatever is (or is not) specified makes sense.*** These fields are dictionaries and in addition to the fields specified above, the following fields are allowed:test-file:# !!str, path to file generated, absolute or relative to the executableexists:# !!bool, flag to check if the file exists. all other checks are ignoredbinary:# !!bool, flag to indicate the file is binaryOther thantest-file, each field is optional, so long as whatever is (or is not) specified makes sense.Basic UsageGiven some configuration you can runcontestusing the following:contest<pathtoconfigurationfile>This will parse the configuration and run the specified test case(s). In the configuration file each test case is defined under thetest-casesnode in the recipe; simply add a new section as desired. You will just need to make sure each test is named uniquely. Here is an example of a test recipe (taken fromexamples/native_console_app):executable:hello_world.exetest-cases:-name:standardstdin:Lnk2paststdout:|Hello! What is your name?Welcome to the world, Lnk2past!Let us break down what this is specifying:The name of theexecutableto run ishello_world.exeWe have a single test-case namedstandardWe define the input tostdin, which is a single entry:Lnk2pastWe define the output tostdout, which is:Hello! What is your name?
Welcome to the world, Lnk2past!This is really the equivalent of the following in some shell environment:~/project>./hello_world.exe
Hello!Whatisyourname?
Lnk2past
Welcometotheworld,Lnk2past!This means that when running the executablehello_world.exewe can expect the input in step 3 to yield the output in step 4.contestdoes this comparison for you! This allows you to write tests that would reflect actual use cases of your executable. Add as many tests as you like to cover various pathways through your program and to cover the various errors your program may encounter.Check out the other examples under theexamplesdirectory.Test Directoriescontestwill run each test-case in a separate directory, and will create those directories in the same directory containing the test recipe. This ensures minimal conflict between test cases. For example, if your test recipe contains test cases "foo" and "bar" and is located in "C:\Users\Lnk2past\MyProject", then you can expect the following directory structure:C:\Users\Lnk2past\MyProject\
|---src\...
|---include\...
|---contest_recipe.yaml
|---test_output\
    |---foo\...
    |---bar\...Even if your test-case produces no output on disk, the test-output directory will be created.Filtering TestsYou can filter your test-recipes to only run a select few. This may be useful during debugging to only run your new test without needing to run the entire test recipe. You can do this via the--filteroption. This expects someregular expressionto filter on. e.g. we can test only those tests that are marked with a specific keyword in their names, say "tracking", by doing the following:contesttest_recipe.yaml--filter"tracking"Likewise, you can exclude specific tests in order if they are problematic or if you are focusing on other tests. simply use the--exclude-filtersor--excludefor short. So long as you knowregexyou can do whatever you like for filtering your tests!