Package: checkPy
checkpyA Python tool for running tests on Python source files. Intended to be
used by students whom are taking courses fromProgramming Lab at the UvA.Check outhttps://github.com/spcourse/testsfor examples of tests.Installationpip install checkpyTo download tests, run checkPy with-das follows:checkpy -d YOUR_GITHUB_TESTS_URLFor instance:checkpy -d spcourse/testsHere spcourse/tests points tohttps://github.com/spcourse/tests. You can also use the full url. This tests repository contains a test forhello.py. Here is how to run it:$ echo 'print("hello world")' > hello.py
$ checkpy hello
Testing: hello.py
:( prints "Hello, world!"
    assert 'hello world\n' == 'Hello, world!\n'
    - hello world
    ? ^
    + Hello, world!
    ? ^    +      +
:) prints exactly 1 line of outputUsageusage: checkpy [-h] [-module MODULE] [-download GITHUBLINK] [-register LOCALLINK] [-update] [-list] [-clean] [--dev]
                [--silent] [--json] [--gh-auth GH_AUTH]
                [files ...]

checkPy: a python testing framework for education. You are running Python version 3.10.6 and checkpy version 2.0.0.

positional arguments:
files                 names of files to be tested

options:
-h, --help            show this help message and exit
-module MODULE        provide a module name or path to run all tests from the module, or target a module for a
                        specific test
-download GITHUBLINK  download tests from a Github repository and exit
-register LOCALLINK   register a local folder that contains tests and exit
-update               update all downloaded tests and exit
-list                 list all download locations and exit
-clean                remove all tests from the tests folder and exit
--dev                 get extra information to support the development of tests
--silent              do not print test results to stdout
--json                return output as json, implies silent
--gh-auth GH_AUTH     username:personal_access_token for authentication with GitHub. Only used to increase  GitHub api's rate limit.To test a single file call:checkpy YOUR_FILE_NAMEAn exampleTests in checkpy are functions with assertions. For instance:fromcheckpyimport*@test()defprintsHello():"""prints Hello, world!"""assertoutputOf()=="Hello, world!\n"checkpy'stestdecorator marks the function below as a test. The docstring is a short description of the test for the student. This test does just one thing, assert that the output of the student's code matches the expected output exactly. checkpy leverages pytest's assertion rewriting to autmatically create assertion messages. For instance, a student might see the following when running this test:$ checkpy hello
Testing: hello.py
:( prints Hello, world!
    assert 'hello world\n' == 'Hello, world!\n'
    - hello world
    ? ^
    + Hello, world!
    ? ^    +      +Writing testsTests are discovered by filename. If you want to test a file calledhello.py, the corresponding test must be namedhelloTest.py. These tests must be placed in a folder calledtests. For instance:tests/helloTest.py. Tests are distributed via GitHub repositories, but for development purposes tests can also be registered locally via the-rflag. For instance:mkdir tests
touch tests/helloTest.py
checkpy -r tests/helloTest.pyOnce registered, checkpy will start looking in that directory for tests. Now we need a test. A test minimally consists of the following:fromcheckpyimport*@test()defprintsHello():"""prints Hello, world!"""assertoutputOf()=="Hello, world!\n"A function marked as a test through checkpy's test decorator. The docstring is a short, generally one-line, description of the test for the student. Then at least one assert.Quick tip, use only binary expressions in assertions and keep them relatively simple for students to understand. If a binary expression is not possible, or you do not want to spoil the output, raise your own assertionError instead:raise AssertionError("Your program did not output the answer to the ultimate question of life, the universe, and everything").While developing, you can run checkpy with the--devflag to get verbose error messages and full tracebacks. So here we might do:$ checkpy --dev hello                   
Testing: hello.py
:( prints "Hello, world!"
assert 'hello world\n' == 'Hello, world!\n'
    - hello world
    ? ^
    + Hello, world!
    ? ^    +      +
:) prints exactly 1 line of outputCheck outhttps://github.com/spcourse/testsfor many examples of checkpy tests.Short examplesDependencies between tests@test()defexactHello():"""prints \"Hello, world!\""""assertoutputOf()=="Hello, world!\n"@failed(exactHello)defoneLine():"""prints exactly 1 line of output"""assertoutputOf().count("\n")==1@passed(exactHello)defallGood():"""Good job, everything is correct! You are ready to hand in."""Test functions@test()deftestSquare():"""square(2) returns 4"""assertgetFunction("square")(4)==4Give hints@test()deftestSquare():"""square(2) returns 4"""assertgetFunction("square")(4)==4,"did you remember to round your output?"Handle randomness with pytest'sapprox@test()deftestThrowDice():"""throw() returns 7 on average"""throw=getFunction("throw")avg=sum(throw()foriinrange(1000))/1000assertavg==approx(7,abs=0.5)Ban language constructsimportast@test()deftestSquare():"""square(2) returns 4"""assertast.Whilenotinstatic.AbstractSyntaxTree()assertgetFunction("square")(4)==4Check types@test()deftestFibonacci():"""fibonacci(10) returns the first ten fibonacci numbers"""fibonacci=getFunction("fibonacci")assertfibonacci(10)==Type(list[int])assertfibonacci(10)==[0,1,1,2,3,5,8,13,21,34]Configure which files should be presentCallingonly,include,exclude,requireordownloadhas checkpy create a temporary directory to which the specified files are copied/downloaded. The tests then run in that directory.only("sentiment.py")download("pos_words.txt","https://github.com/spcourse/text/raw/main/en/sentiment/pos_words.txt")download("neg_words.txt","https://github.com/spcourse/text/raw/main/en/sentiment/neg_words.txt")@test()deftestPositiveSentiment():"""recognises a positive sentence"""...Change the timeout@test(timeout=60)defexactHello():"""prints \"Hello, world!\""""assertoutputOf()=="Hello, world!\n"Short declarative testsThis is a new style of tests for simple repetative use cases. Be sure to check outhttps://github.com/spcourse/testsfor many more examples. For examplesentimentTest.pycorrectForPos=test()(declarative.function("sentiment_of_text").params("text").returnType(int).call("Coronet has the best lines of all day cruisers.").returns(1).description("recognises a positive sentence"))Distributing testscheckpy downloads tests directly from Github repos. The requirement is that a folder calledtestsexists within the repo that contains only tests and folders (which checkpy treats as modules). There must also be at least one release in the Github repo. checkpy will automatically target the latest release. To download tests call checkpy with the optional-dargument and pass your github repo url. checkpy will automatically keep tests up to date by checking for any new releases on GitHub.Testing checkpypython3 run_tests.py