1212
Add missing test coverage for exception handling in `scanapi/scan.py` · Issue #881 · scanapi/scanapi · GitHub
Skip to content

Add missing test coverage for exception handling in scanapi/scan.py #881

@camilamaia

Description

@camilamaia

⚠️ Reserved for Conference Tutorial

This issue is currently reserved for participants of a tutorial at PyCon DE & PyData 2026. It is part of a guided session, so contributions may be coordinated during the event. If the issue is not resolved during the tutorial, it will be reopened for general contributions afterward. Thank you for your understanding and interest in contributing 💜

Description

There is currently missing test coverage for the exception handling block in scanapi/scan.py, specifically:

except (BadConfigurationError, InvalidPythonCodeError) as e:
    logger.error(e)
    raise SystemExit(ExitCode.USAGE_ERROR)

According to Codecov, this part of the code is not being exercised by the test suite:
https://app.codecov.io/gh/scanapi/scanapi/blob/main/scanapi%2Fscan.py

Goal

Add tests that ensure this exception handling path is properly covered.

What needs to be done

  • Add tests in tests/unit/test_scan.py

  • Create scenarios that simulate:

    • BadConfigurationError
    • InvalidPythonCodeError
  • Verify that:

    • The error is logged (optional but recommended)
    • The program exits with SystemExit
    • The exit code matches ExitCode.USAGE_ERROR

Suggested approach

  • Use pytest.raises(SystemExit) to assert the exit behavior
  • Mock or trigger scenarios that raise the targeted exceptions
  • Optionally use caplog to assert logging behavior

Acceptance criteria

  • Both exception branches are covered by tests
  • Coverage increases for scanapi/scan.py
  • Tests are clear and easy to understand for new contributors

Additional context

  • Tests should be added to: tests/unit/test_scan.py
  • Testing guidelines are available at: wiki/Writing-Tests.md

This is a great issue for contributors who want to get familiar with:

  • Exception handling in CLI tools
  • Writing tests with pytest
  • Improving code coverage

Metadata

Metadata

Assignees

No one assigned

    Labels

    Code QualityImpacts the code quality: tests, logs, alerts...Good First IssueGood for newcomers or first time contributors.PyCon DE 2026Issues marked with this label are specifically intended for participants of the PyCon DE tutorial.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions