Package: cg-pytest-reporter
cg-pytest-reporterA Pytest reporter plugin for CodeGrade AutoTest v2.This plugin writes messages to CodeGrade AutoTest v2's structured output channel.
For each test that was run aunit-testmessage is written as described in
CodeGrade's documentation. When all tests have finished running, a final message
is written with the amount of points that were achieved in the test run.Utility functionsThis plugin comes with a few utility functions to easily modify certain aspects
of each test case or test suite. All of these are decorators that you can apply
directly to your test functions or classes.Test suite decoratorsThese decorators change properties of an entire test suite. In terms of Pytest
that is a single file containing test functions, or a single class within such a
file.suite_nameWith thesuite_nameyou can modify the name of a test suite that will be
displayed in the AutoTest v2 UI.fromcg_pytest_reporterimportsuite_name@suite_name('My Test Suite')classTestSuite:deftest_function():assertTrueBecause it is not possible to decorate an entire module, it is possible to
change the name of the module-level suite by setting the__cg_suite_name__variable on the module.__cg_suite_name__='My Test Suite'deftest_function():assertTruesuite_weightUse thesuite_weightdecorator to change the weight of an entire suite
relative to other suites in your test run.fromcg_pytest_reporterimportsuite_weight@suite_weight(2)classTestSuite:deftest_function():assertTrueSimilar to the suite name, you can set the weight of the module-level suite with
the__cg_suite_weight__variable.__cg_suite_weight__=2deftest_function():assertTrueTest function decoratorsThese decorators modify the behaviour of a single test case.Although they work on the test case level, they can be applied to a class
containing test functions. This works as if the decorator was applied to each
test function individually, but each decorator can still be overridden on
individual test cases. It is mentioned on each of the decorators where this
might be useful.nameWith thenamedecorator you can change the name of a single test case.fromcg_pytest_reporterimportname@name('My Cool Test')deftest_function():assertTruedescriptionWith thedescriptiondecorator you can set a description for a single test
case. This can be useful if the name of the test function is not descriptive
enough.fromcg_pytest_reporterimportdescription@description('A somewhat longer description of what is being tested.')deftest_function():assertTrueweightChange the weight of a single test function relative to other test functions
within the same suite.fromcg_pytest_reporterimportweight@weight(2)deftest_function():assertTrueThis decorator can also be applied to a test class, in which case it will set a
default weight for all tests within that class which can still be overridden for
individual tests within that class. This can be useful for example if you want
most tests within a class not to count towards the score.fromcg_pytest_reporterimportweight@weight(0)classTestClass:# This test case will have a weight of 0 applied, and as such will not count# towards the achieved score.deftest_function():assertTrue# This test case will still count towards the score with the default weight# of 1.@weight(1)deftest_something_else():assertTruereasonChange the reason of failure of the test case. This can be used to give students
a hint where their code is likely to break.fromcg_pytest_reporterimportreasonfromfibonacciimportfibonacci@reason('Did you start counting at the correct index? (off-by-one error)')deftest_fibonacci():assertfibonacci(10)==55hide_stdoutWhen a test fails, thestdoutthat was written while the test was running is
sent along with the result. With thehide_stdoutdecorator you can prevent thestdoutfrom being sent.fromcg_pytest_reporterimporthide_stdout# The string "Hello World!" that was printed to `stdout` will not be sent along# with the results.@hide_stdoutdeftest_failure():print('Hello World!')assertFalseThis decorator can also be applied to a test class to hide thestdoutof each
test case within it.fromcg_pytest_reporterimporthide_stdout@hide_stdoutclassTestClass:deftest_failure():print('Hello World!')assertFalsedeftest_ok():print('Hello World!')hide_stderrThis works the same ashide_stdoutexcept that it will hide thestderrchannel instead.importsysfromcg_pytest_reporterimporthide_stderr# The string "Hello World!" that was printed to `stderr` will not be sent along# with the results.@hide_stderrdeftest_failure():print('Hello World!',file=sys.stderr)assertFalseThis decorator can also be applied to a test class to hide thestderrof each
test case within it.importsysfromcg_pytest_reporterimporthide_stderr@hide_stderrclassTestClass:deftest_failure():print('Hello World!',file=sys.stderr)assertFalsedeftest_ok():print('Hello World!',file=sys.stderr)hide_outputA combined version ofhide_stdoutandhide_stderr.importsysfromcg_pytest_reporterimporthide_output# The string "Hello World!" that was printed to `stdout` or `stderr` will not be# sent along with the results.@hide_outputdeftest_failure():print('Hello World!')print('Hello World!',file=sys.stderr)assertFalseThis decorator can also be applied to a test class to hide the output of each
test case within it.importsysfromcg_pytest_reporterimporthide_output@hide_outputclassTestClass:deftest_failure():print('Hello World!',file=sys.stderr)assertFalsedeftest_ok():print('Hello World!')