loading...

Setting up Python unittests with GitHub annotations

rdil profile image Reece Dunham ・1 min read

Hello!
This is my first post on DEV, so I'm going to try to make this quick and simple.

If you want inline examples of exactly where your code is failing, you can integrate Cirrus CI with GitHub annotations. This is super simple to do.

  1. Start off by writing unittests. This is super simple.
  2. Setup a basic CI pipeline (.cirrus.yml file). You will want to do something like this:
tests_task:
  # define Docker container
  container:
    image: python:latest

  # install project requirements and the annotation result builder
  install_script: |
    pip install -r ./some-requirements-file.txt
    pip install unittest-xml-reporting

  # normally, you would run unittests with the main command
  # we need to build XML reports, so use this command
  script: python3 -m xmlrunner tests
  # replace tests with the name of the module your unittests are in

  # (always) upload results - even if the tests fail
  always:
    unittest_results_artifacts:
      # where the outputted XML files are
      path: ./*.xml
      # required, even though it sounds wrong
      format: junit

And that is all you need to do!
You should then get annotations.

Have a nice day!

Discussion

pic
Editor guide