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 Unit Testing Workflow using PyTest #400

Merged
merged 4 commits into from
Jul 27, 2024
Merged
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
44 changes: 44 additions & 0 deletions .github/workflows/testing-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test using Pytest

on:
push:
branches:
- main
pull_request:

jobs:
tests:
name: "Run PyTest"
runs-on: ubuntu-22.04
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: "${{ matrix.python-version }}"
cache: "pip"

- name: Install dependencies
run: |
sudo apt install -y libegl1 libxkbcommon0
pip install pytest pytest-md pytest-emoji
- name: Install ProtonUp-Qt
run: pip install -e .

- name: Run pytest
uses: pavelzw/pytest-action@v2
env:
QT_QPA_PLATFORM: "offscreen"
with:
verbose: true
emoji: true
job-summary: true
custom-arguments: '-q'
click-to-expand: true
report-title: 'ProtonUp-Qt Test Report'
22 changes: 22 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from pupgui2.util import *

from pupgui2.datastructures import SteamApp, LutrisGame, HeroicGame


def test_get_random_game_name():
""" test whether get_random_game_name returns a valid game name """
names = ["game", "A super cool game", "A game with a very long name that is very long", "0123456789"]

steam_app = [SteamApp() for _ in range(len(names))]
lutris_game = [LutrisGame() for _ in range(len(names))]
heroic_game = [HeroicGame() for _ in range(len(names))]

for i, name in enumerate(names):
steam_app[i].game_name = name
lutris_game[i].name = name
heroic_game[i].title = name

for i in range(10):
assert get_random_game_name(steam_app) in names
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is just "regular" Python code, but PyTest will read and run these functions, and collate the rest results, tell you where certain tests fell over and why (e.g. expected X list of values, got Y). Is that right?

That means for "basic" tests there is not always a requirement to import or use anything from PyTest? There is probably some specific PyTest stuff for mocking and such, but it was interesting to me to see this as just simple Python code! That would be a big question mark I had around unit testing out of the way...

I'm definitely hoping to learn a lot more about PyTest and to go back and write unit tests for the code I've contributed, so figured I'd ask here 😄

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is just "regular" Python code, but PyTest will read and run these functions, and collate the rest results, tell you where certain tests fell over and why (e.g. expected X list of values, got Y). Is that right?
That means for "basic" tests there is not always a requirement to import or use anything from PyTest?

That is correct.
PyTest imports are only required for e.g. fixtures which provide reliable/constant example data.
It will look for files prefixed with test_ by default.

I'm definitely hoping to learn a lot more about PyTest and to go back and write unit tests for the code I've contributed, so figured I'd ask here 😄

I have the same goal. I've worked with tools like Vitest before but I'm also new to PyTest 😄

There are also a few things I still need to figure out. For example, I'm calling the assert statement 30 times. I've seen that (with Java I think) and it definitively works as it will just throw an AssertionError in case the parameter is False. But I don't know yet whether that is the correct/best way to do so.

assert get_random_game_name(lutris_game) in names
assert get_random_game_name(heroic_game) in names