-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
assert get_random_game_name(lutris_game) in names | ||
assert get_random_game_name(heroic_game) in names |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 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 anAssertionError
in case the parameter isFalse
. But I don't know yet whether that is the correct/best way to do so.