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

Add Unit Testing Workflow using PyTest #400

merged 4 commits into from
Jul 27, 2024

Conversation

DavidoTek
Copy link
Owner

@DavidoTek DavidoTek commented May 18, 2024

Fix #347

  • Adds a CI workflow for Unit Testing using pytest
    • Runs on Pull Requests and Pushes to main
    • Currently tests on Ubuntu 22.04 and Python 3.10
  • Adds an initial test for util.py#get_random_game_name
  • Produces Markdown Test Report using pytest-md

Fixed Qt import for PyTest by installing libEGL and libxcbcommon and setting QT_QPA_PLATFORM=offscreen

Previously: Currently fails due to missing libEGL, i.e. the runner is headless but the Qt imports expect a graphics library. I will investigate that.

==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_util.py ______________________
ImportError while importing test module '/home/runner/work/ProtonUp-Qt/ProtonUp-Qt/tests/test_util.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_util.py:1: in <module>
    from pupgui2.util import *
pupgui2/util.py:20: in <module>
    from PySide6.QtWidgets import QApplication, QStyleFactory, QMessageBox, QCheckBox
E   ImportError: libEGL.so.1: cannot open shared object file: No such file or directory
---------------- generated Markdown report: /tmp/tmp.t8xQFKNH8p ----------------

@DavidoTek DavidoTek marked this pull request as draft May 18, 2024 19:21
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.

@DavidoTek DavidoTek marked this pull request as ready for review July 27, 2024 12:00
@DavidoTek DavidoTek merged commit cd3e843 into main Jul 27, 2024
1 check passed
@sonic2kk
Copy link
Contributor

sonic2kk commented Jul 27, 2024

Woohoo, once v2.10.0 is out and I get some of the ideas I have off my plate (I have some planned PRs for the near-ish future), I'll definitely see about submitting unit tests!

@sonic2kk
Copy link
Contributor

Do we need to add PyTest as a dependency in requirements.txt? On one hand it's not needed for actually bundling the AppImage and Flatpak, but it is needed for local development. I'm not sure how other projects handle this, maybe this is just something for a future CONTRIBUTING.md file to note that PyTest should be installed following the steps in their Documentation.

@DavidoTek
Copy link
Owner Author

Do we need to add PyTest as a dependency in requirements.txt?

Good question. Following what I read online, we may create a requirements_dev.txt or similar file. I'm unsure whether we want to do that for a single dependency.
The CONTRIBUTING.md method you mentioned is also okay I guess.

@sonic2kk
Copy link
Contributor

sonic2kk commented Aug 1, 2024

It looks like PyTest has a lot of plugins, some of which we might find useful.

Outside of that, we may also want to use requests-mock, or alternatively the responses library - I would probably lean towards using responses, from looking over the usage it seems very clean and very natural and complementory to the requests usage!

Adding all of these all at once may not make much sense, but in order to make sure these are available on a user's machine (so they don't have to hunt through the code to see what we might be importing, especially as the test suite get more robust) and in order to make sure our test dependencies are available on CI, it could be an idea to make a requirements_dev.txt.

@DavidoTek
Copy link
Owner Author

Good to know.

Outside of that, we may also want to use requests-mock, or alternatively the responses library - I would probably lean towards using responses, from looking over the usage it seems very clean and very natural and complementory to the requests usage!

Ah, wasn't aware of responses. Seems to be the one which is used more often (4.1k stars and 24M downloads per month).

it could be an idea to make a requirements_dev.txt

Yes, we should create that file and add dependencies one by one when we need them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce Tests into ProtonUp-Qt
2 participants