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

refactor: Add unit testing and refactor code to follow Python standards. #31

Merged
merged 3 commits into from
Feb 27, 2024

Conversation

bvanelli
Copy link
Contributor

@bvanelli bvanelli commented Feb 5, 2024

Hello @cybcon , I started writing this code on the last Hackergarten, but did not had the time to push it yet.

You wrote the integration test with the other part of the code. This tests how the applications work together, but it's too broad to test small functions inside the code. This PR adds the basic structure for unit testing too.

I also included other changes, here is the full changelog:

  • Reformat file with community standards black, ruff and isort, with basic config. Black and ruff are formatters that make sure the code follows a stricter coding style. isort on the other hand makes sure the imports are sorted correctly.
  • Format all variable namings using PEP8. Here, I mostly took the suggestions from Pycharm, and some of it was done directly by black + ruff.
  • Add two example unit tests for the part of the code that is currently testable.
  • Add testing on CI using github actions and pytest. This makes sure the test would run on pull requests and on main branch.
  • Improve code clarity by using f-strings, available since python 3.6. This should keep the complexity of writing custom logs much lower and improve readability.

I had to change some of the logic to make sure the code was testable by reducing the functions complexity, so some functions might be slightly different.

Let me know what you think and what you suggest.

- Reformat file with community standards black and ruff, with basic
config
- Format all variable namings using PEP8.
- Add unit tests for two of the auxiliary methods.
- Add testing on CI using github actions and pytest
- Improve code clarity by using f-strings, available since python 3.6
@cybcon cybcon added the patch Fixes label Feb 6, 2024
src/app/modbus_server.py Outdated Show resolved Hide resolved
src/app/modbus_server.py Outdated Show resolved Hide resolved
src/app/modbus_server.py Outdated Show resolved Hide resolved
src/app/modbus_server.py Outdated Show resolved Hide resolved
.github/workflows/test.yaml Outdated Show resolved Hide resolved
@cybcon
Copy link
Owner

cybcon commented Feb 6, 2024

Hi @bvanelli
Thank you for this great contribution!

Copy link
Owner

@cybcon cybcon left a comment

Choose a reason for hiding this comment

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

Python test pipeline still fail.
In step pytest we get following error:

============================= test session starts ==============================
platform linux -- Python 3.9.18, pytest-8.0.0, pluggy-1.4.0
rootdir: /home/runner/work/modbus-server/modbus-server
collected 0 items / 2 errors

==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_server.py _____________________
ImportError while importing test module '/home/runner/work/modbus-server/modbus-server/tests/test_server.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_server.py:2: in <module>
    from src.app.modbus_server import prepare_register
src/app/modbus_server.py:17: in <module>
    from pymodbus.datastore import (
E   ModuleNotFoundError: No module named 'pymodbus'
_____________________ ERROR collecting tests/test_utils.py _____________________
ImportError while importing test module '/home/runner/work/modbus-server/modbus-server/tests/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_utils.py:2: in <module>
    from src.app.modbus_server import get_ip_address
src/app/modbus_server.py:17: in <module>
    from pymodbus.datastore import (
E   ModuleNotFoundError: No module named 'pymodbus'
=========================== short test summary info ============================
ERROR tests/test_server.py
ERROR tests/test_utils.py
!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!
============================== 2 errors in 0.10s ===============================

.github/workflows/test.yaml Outdated Show resolved Hide resolved
@bvanelli
Copy link
Contributor Author

Python test pipeline still fail. In step pytest we get following error:

============================= test session starts ==============================
platform linux -- Python 3.9.18, pytest-8.0.0, pluggy-1.4.0
rootdir: /home/runner/work/modbus-server/modbus-server
collected 0 items / 2 errors

==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_server.py _____________________
ImportError while importing test module '/home/runner/work/modbus-server/modbus-server/tests/test_server.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_server.py:2: in <module>
    from src.app.modbus_server import prepare_register
src/app/modbus_server.py:17: in <module>
    from pymodbus.datastore import (
E   ModuleNotFoundError: No module named 'pymodbus'
_____________________ ERROR collecting tests/test_utils.py _____________________
ImportError while importing test module '/home/runner/work/modbus-server/modbus-server/tests/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_utils.py:2: in <module>
    from src.app.modbus_server import get_ip_address
src/app/modbus_server.py:17: in <module>
    from pymodbus.datastore import (
E   ModuleNotFoundError: No module named 'pymodbus'
=========================== short test summary info ============================
ERROR tests/test_server.py
ERROR tests/test_utils.py
!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!
============================== 2 errors in 0.10s ===============================

I had the conditional wrong, could you try again?

@cybcon cybcon enabled auto-merge (squash) February 27, 2024 13:19
@cybcon cybcon disabled auto-merge February 27, 2024 13:20
@cybcon cybcon merged commit a81fe83 into cybcon:main Feb 27, 2024
19 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants