Run Python Linter and Tests
untested
not tested
Example Module
main.py
"""Main Module"""
class Foobar:
"""Foobar"""
def __init__(self):
self.dict = {}
def __setitem__(self, key, value):
self.dict[key] = value
def __getitem__(self, item):
return self.dict[item]
def __repr__(self):
return self.dict.__repr__()
if __name__ == "__main__":
foobar = Foobar()
foobar['foo'] = 'bar'
print(foobar)
print(foobar['foo'])
Example Test
context.py
import os
import sys
import unittest
import pytest
import logging
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
logger = logging.getLogger(__name__)
from src.main import Foobar
logger.setLevel(logging.DEBUG)
@pytest.fixture(name="foobar_fixture")
def foobar_fixture():
return Foobar()
test_main.py
from context import *
def test_foobar(foobar_fixture: Foobar):
foobar_fixture['foo'] = 'bar'
assert foobar_fixture['foo'] == 'bar'
Example workflow
name: Python Lint and Test
on:
pull_request:
branches:
- main
push:
branches:
- main
tags:
- '*.*.*'
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [ 3.12 ]
# python-version: [ 3.8, 3.9, 3.10, 3.11, 3.12 ]
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install poetry
poetry install
- name: Run PyLint
run: |
{
echo 'LINTER<<EOF'
poetry run pylint sandbox_9000 --exit-zero --output-format=colorized
echo EOF
} >> "$GITHUB_ENV"
- name: Run PyTest Using Coverage
run: |
poetry run pytest
poetry run coverage run --source sandbox_9000 -m pytest
{
echo 'COVERAGE<<EOF'
poetry run coverage report -m --format=markdown
echo EOF
} >> "$GITHUB_ENV"
- name: Linter and Coverage Results
run: |
echo "*** Linter ***"
echo $LINTER
echo "*** Coverage ***"
echo $COVERAGE
- name: Comment on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
# github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## Linter\n ${{ env.LINTER }}\n\n## Coverage\n\n${{ env.COVERAGE }}`
})
Was this page helpful?