Create a Pytest unit test

Create a Pytest unit test by writing your tests directly in the Pytest block

When working with Python, Pytest is an invaluable tool to help you test your students' code. You can easily create a Pytest unit test using the Pytest block in AutoTest v2.

The advantage of using the Pytest block:

  • Write your tests in CodeGrade The Pytest block allows your to write your tests directly within the code editor provided in the block. This makes it very easy to adjust your tests on-the-go rather than having to download and reupload your unit test script.

  • No installation steps The block automatically installs Pytest in the VM for you.

How to use the Pytest block in AutoTest v2:

  1. Navigate to the Tests portion of the AutoTest settings.

  2. Click the Pytest block to add it to the bottom of your test configuration or drag-and-drop it to the desired location.

  3. Select the version of Pytest version you would like to use in your tests using the drop-down menu.

  4. Write (or copy-and-paste) your tests directly in the provided editor. This will be interpreted as if it were in a test.py file.

How to use the cg-pytest reporter:

The Pytest block makes use of the cg-pytest-reporter module. This modules enables a number of useful decorators that you can use to customize your tests and test suites. Here are some examples:

  • Test name The @name decorator lets you specify a name for your test rather than using the function name.

    from cg_pytest_reporter import name
    
    
    @name('My Cool Test')
    def test_function():
        assert True
  • Test description The @description decorator lets you specify a description for your test to give your students more clarity about what is actually being tested.

    from cg_pytest_reporter import description
    
    
    @description('A somewhat longer description of what is being tested.')
    def test_function():
        assert True
  • Test weight The @weight decorator lets you specify a weight for individual tests.

    from cg_pytest_reporter import weight
    
    
    @weight(2)
    def test_function():
        assert True

Last updated