Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of Python Pytest test runner with Junit output file #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,25 @@ jobs:
with:
paths: javascript-tap/results/**/*.tap
if: always()

# This runs Python tests using the Pytest test framework
pytest-junit:
name: Python / pytest
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: pip install pytest
- name: Run test
run: pytest --junit-xml=results/pytest-junit.xml
working-directory: pytest-junit
- name: Create test summary
uses: test-summary/action@v1
with:
paths: pytest-junit/results/*.xml
if: always()
38 changes: 38 additions & 0 deletions pytest-junit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test-summary example: Python with pytest
========================================

This produces a test summary for a Python project using the Pytest test framework.

GitHub Actions Workflow
-----------------------

An example GitHub Actions workflow that builds a Python project, runs the tests, runs the test-summary action and uploads the test summary markdown as an artifact.

```yaml
name: Build and Test Python

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build:
name: Build and Test
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Build and test
run: |
npm ci
npm run test
- name: Create test summary
uses: test-summary/action@dist
with:
paths: results/**/*.xml
if: always()
```
1 change: 1 addition & 0 deletions pytest-junit/directory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
5 changes: 5 additions & 0 deletions pytest-junit/directory/test_file_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-


def test_success():
assert True
5 changes: 5 additions & 0 deletions pytest-junit/directory/test_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-


def test_success():
assert True
Empty file added pytest-junit/pytest.ini
Empty file.
3 changes: 3 additions & 0 deletions pytest-junit/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

print("hello world")
78 changes: 78 additions & 0 deletions pytest-junit/test_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-


class TestClassPassing(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
pass

def test_passing(self):
assert True


class TestClassFailing(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
pass

def test_failing(self):
assert False


class TestClassError(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
pass

def test_error(self):
1 / 0


class TestClassFailingAndErrorTeardown(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
1 / 0

def test_error(self):
assert False


class TestClassErrorSetup(object):
def setup_method(self, method):
1 / 0

def teardown_method(self, method):
pass

def test_passing(self):
assert True


class TestClassErrorSetupAndTeardown(object):
def setup_method(self, method):
1 / 0

def teardown_method(self, method):
1 / 0

def test_passing(self):
assert True


class TestClassErrorTeardown(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
1 / 0

def test_passing(self):
assert True
20 changes: 20 additions & 0 deletions pytest-junit/test_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-

import pytest


def test_success():
assert True


def test_fails():
assert False


@pytest.mark.parametrize("number", list(range(3)))
def test_fixtures(number):
assert number % 2 == 0


def test_error():
1 / 0
39 changes: 39 additions & 0 deletions pytest-junit/test_module_setup_teardown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-


def setup_module(module):
pass


def teardown_module(module):
print("TD MO")


def test_passing():
assert True


def test_failing():
assert False


class TestClassPassing(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
pass

def test_passing(self):
assert True


class TestClassFailing(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
print("TD M")

def test_failing(self):
assert False
23 changes: 23 additions & 0 deletions pytest-junit/test_skip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-

import pytest


@pytest.mark.skip(reason="Skip")
def test_skip_function():
pass


class TestSkipCall(object):
@pytest.mark.skip(reason="Skip")
def test_skip_method(self):
pass


@pytest.mark.skip(reason="Skip")
class TestSkipClass(object):
def test_skipped_1(self):
pass

def test_skipped_2(self):
pass
9 changes: 9 additions & 0 deletions pytest-junit/test_slow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-

import time


def test_slow_passing():
time.sleep(2)

assert True
55 changes: 55 additions & 0 deletions pytest-junit/test_std.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-

import sys


def test_stdout():
print("STDOUT")


def test_stderr():
sys.stderr.write("STDERR\n")


class TestClassStdout(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
pass

def test_stdout(self):
print("STDOUT")


class TestClassStdoutSetup(object):
def setup_method(self, method):
print("SETUP")

def teardown_method(self, method):
pass

def test_stdout(self):
pass


class TestClassStdoutAllPhases(object):
def setup_method(self, method):
print("SETUP")

def teardown_method(self, method):
print("TEARDOWN")

def test_stdout(self):
print("TEST")


class TestClassFailing(object):
def setup_method(self, method):
pass

def teardown_method(self, method):
pass

def test_stderr(self):
sys.stderr.write("STDERR\n")
16 changes: 16 additions & 0 deletions pytest-junit/test_unittest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

import unittest


class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual("foo".upper(), "FOO")

def test_isupper(self):
self.assertTrue("FOO".isupper())
self.assertTrue("Foo".isupper())


if __name__ == "__main__":
unittest.main()