Skip to content

refactor(test): Run tests with pytest#1629

Closed
joelspadin wants to merge 1 commit intozmkfirmware:mainfrom
joelspadin:pytest
Closed

refactor(test): Run tests with pytest#1629
joelspadin wants to merge 1 commit intozmkfirmware:mainfrom
joelspadin:pytest

Conversation

@joelspadin
Copy link
Copy Markdown
Collaborator

Replaced the run-test.sh script with a Python script that runs our unit tests through pytest. Tests are now run in parallel to speed up running the entire test suite, and it allows for integration with other tools that support pytest, such as IDEs.

Time to build and run the entire test suite on my i5-13600k running in WSL:

Serial 22m10.805s
Parallel 4m18.305s

@petejohanson
Copy link
Copy Markdown
Contributor

Tried to install the deps within our dev containers, and it had issues, and couldn't run the tests after:

root@ed9c7ab05fdf:/workspaces/zmk/app# pip3 install -r scripts/requirements.txt 
Collecting remarshal>=0.14.0
  Using cached remarshal-0.14.0-py3-none-any.whl (6.0 kB)
Collecting jsonschema>=3.2.0
  Downloading jsonschema-4.17.3-py3-none-any.whl (90 kB)
     |████████████████████████████████| 90 kB 2.9 MB/s 
Collecting pythonsed>=2.1
  Downloading pythonsed-2.1.post0-py2.py3-none-any.whl (34 kB)
Requirement already satisfied: pytest>=7.0.0 in /usr/local/lib/python3.8/dist-packages (from -r scripts/requirements.txt (line 12)) (7.2.0)
Collecting pytest-xdist>=3.0.0
  Downloading pytest_xdist-3.1.0-py3-none-any.whl (36 kB)
Collecting tomlkit<0.8,>=0.7
  Using cached tomlkit-0.7.2-py2.py3-none-any.whl (32 kB)
Collecting cbor2<6.0,>=5.1
  Downloading cbor2-5.4.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (236 kB)
     |████████████████████████████████| 236 kB 21.7 MB/s 
Requirement already satisfied: python-dateutil<3.0,>=2.8 in /usr/local/lib/python3.8/dist-packages (from remarshal>=0.14.0->-r scripts/requirements.txt (line 5)) (2.8.2)
Collecting u-msgpack-python<3.0,>=2.6
  Downloading u_msgpack_python-2.7.2-py2.py3-none-any.whl (10.0 kB)
Collecting PyYAML<6.0,>=5.3
  Downloading PyYAML-5.4.1-cp38-cp38-manylinux1_x86_64.whl (662 kB)
     |████████████████████████████████| 662 kB 20.4 MB/s 
Collecting importlib-resources>=1.4.0; python_version < "3.9"
  Downloading importlib_resources-5.10.2-py3-none-any.whl (34 kB)
Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.8/dist-packages (from jsonschema>=3.2.0->-r scripts/requirements.txt (line 8)) (22.1.0)
Collecting pkgutil-resolve-name>=1.3.10; python_version < "3.9"
  Downloading pkgutil_resolve_name-1.3.10-py3-none-any.whl (4.7 kB)
Collecting pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0
  Downloading pyrsistent-0.19.3-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 6.1 MB/s 
Requirement already satisfied: tomli>=1.0.0; python_version < "3.11" in /usr/local/lib/python3.8/dist-packages (from pytest>=7.0.0->-r scripts/requirements.txt (line 12)) (2.0.1)
Requirement already satisfied: exceptiongroup>=1.0.0rc8; python_version < "3.11" in /usr/local/lib/python3.8/dist-packages (from pytest>=7.0.0->-r scripts/requirements.txt (line 12)) (1.0.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.8/dist-packages (from pytest>=7.0.0->-r scripts/requirements.txt (line 12)) (21.3)
Requirement already satisfied: iniconfig in /usr/local/lib/python3.8/dist-packages (from pytest>=7.0.0->-r scripts/requirements.txt (line 12)) (1.1.1)
Requirement already satisfied: pluggy<2.0,>=0.12 in /usr/local/lib/python3.8/dist-packages (from pytest>=7.0.0->-r scripts/requirements.txt (line 12)) (1.0.0)
Collecting execnet>=1.1
  Downloading execnet-1.9.0-py2.py3-none-any.whl (39 kB)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil<3.0,>=2.8->remarshal>=0.14.0->-r scripts/requirements.txt (line 5)) (1.16.0)
Collecting zipp>=3.1.0; python_version < "3.10"
  Downloading zipp-3.11.0-py3-none-any.whl (6.6 kB)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.8/dist-packages (from packaging->pytest>=7.0.0->-r scripts/requirements.txt (line 12)) (3.0.9)
ERROR: pyocd 0.34.2 has requirement pyyaml<7.0,>=6.0, but you'll have pyyaml 5.4.1 which is incompatible.
ERROR: cmsis-pack-manager 0.4.0 has requirement pyyaml<7.0,>=6.0, but you'll have pyyaml 5.4.1 which is incompatible.
Installing collected packages: tomlkit, cbor2, u-msgpack-python, PyYAML, remarshal, zipp, importlib-resources, pkgutil-resolve-name, pyrsistent, jsonschema, pythonsed, execnet, pytest-xdist
  Attempting uninstall: PyYAML
    Found existing installation: PyYAML 6.0
    Uninstalling PyYAML-6.0:
      Successfully uninstalled PyYAML-6.0
Successfully installed PyYAML-5.4.1 cbor2-5.4.6 execnet-1.9.0 importlib-resources-5.10.2 jsonschema-4.17.3 pkgutil-resolve-name-1.3.10 pyrsistent-0.19.3 pytest-xdist-3.1.0 pythonsed-2.1.post0 remarshal-0.14.0 tomlkit-0.7.2 u-msgpack-python-2.7.2 zipp-3.11.0
root@ed9c7ab05fdf:/workspaces/zmk/app# west test
================================================= test session starts ==================================================
platform linux -- Python 3.8.10, pytest-7.2.0, pluggy-1.0.0
rootdir: /workspaces/zmk/app
plugins: xdist-3.1.0
gw0 [0] / gw1 [0] / gw2 [0] / gw3 [0]                                                                                  

======================================================== ERRORS ========================================================
_____________________________________________ ERROR collecting test_zmk.py _____________________________________________
test_zmk.py:25: in <module>
    def check_call_teed(args: Sequence[str | Path], **kwargs):
E   TypeError: unsupported operand type(s) for |: 'type' and 'type'
_____________________________________________ ERROR collecting test_zmk.py _____________________________________________
test_zmk.py:25: in <module>
    def check_call_teed(args: Sequence[str | Path], **kwargs):
E   TypeError: unsupported operand type(s) for |: 'type' and 'type'
_____________________________________________ ERROR collecting test_zmk.py _____________________________________________
test_zmk.py:25: in <module>
    def check_call_teed(args: Sequence[str | Path], **kwargs):
E   TypeError: unsupported operand type(s) for |: 'type' and 'type'
_____________________________________________ ERROR collecting test_zmk.py _____________________________________________
test_zmk.py:25: in <module>
    def check_call_teed(args: Sequence[str | Path], **kwargs):
E   TypeError: unsupported operand type(s) for |: 'type' and 'type'
=============================================== short test summary info ================================================
ERROR test_zmk.py - TypeError: unsupported operand type(s) for |: 'type' and 'type'
ERROR test_zmk.py - TypeError: unsupported operand type(s) for |: 'type' and 'type'
ERROR test_zmk.py - TypeError: unsupported operand type(s) for |: 'type' and 'type'
ERROR test_zmk.py - TypeError: unsupported operand type(s) for |: 'type' and 'type'
================================================== 4 errors in 0.91s ===================================================
root@ed9c7ab05fdf:/workspaces/zmk/app# 

@petejohanson
Copy link
Copy Markdown
Contributor

Similar error on my local host:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
cmsis-pack-manager 0.5.1 requires pyyaml<7.0,>=6.0, but you have pyyaml 5.4.1 which is incompatible.
pyocd 0.34.3 requires pyyaml<7.0,>=6.0, but you have pyyaml 5.4.1 which is incompatible.

@joelspadin
Copy link
Copy Markdown
Collaborator Author

I don't think this conflict is related to my changes. Remarshal requires PyYAML ^5.3 while cmsis-pack-manager requires PyYAML >=6.0,<7. To resolve that, we need to get rid of the dependency on remarshal, which appears to be unmaintained since October 2020.

@joelspadin
Copy link
Copy Markdown
Collaborator Author

| for types was added in Python 3.10. Switched to Union to support Python 3.8.

@joelspadin joelspadin force-pushed the pytest branch 5 times, most recently from 55473fd to 53a8d96 Compare January 20, 2023 07:12
Copy link
Copy Markdown
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

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

We should update https://zmk.dev/docs/development/tests with info on installing the now required Python deps in order to run the tests.

Comment on lines -65 to +66
run: west test tests/${{ matrix.test }}
run: west test ${{ matrix.test }}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We'll need to install the new Python deps before we run out tests here too. See the current CI failures.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I tried that, but the container doesn't have pip, so it will also fail if I try to install them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, we install pip to our build image then remove it again, to keep that image size small:

RUN \
  apt-get -y update \
  && if [ "$(uname -m)" = "x86_64" ]; then gcc_multilib="gcc-multilib"; else gcc_multilib=""; fi \
  && apt-get -y install --no-install-recommends \
  ccache \
  file \
  gcc \
  "${gcc_multilib}" \
  git \
  gperf \
  make \
  ninja-build \
  python3 \
  python3-dev \
  python3-pip \
  python3-setuptools \
  python3-wheel \
  && pip3 install \
  -r https://raw.githubusercontent.com/zephyrproject-rtos/zephyr/v${ZEPHYR_VERSION}/scripts/requirements-base.txt \
  && pip3 install cmake \
  && apt-get remove -y --purge \
  python3-dev \
  python3-pip \
  python3-setuptools \
  python3-wheel \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

We could either:

  1. Install pip again, then install the deps, or
  2. Add pytest to our Docker build images (I'm curious the change in image size, I can check this), or
  3. Switch to the dev image just for this.

I'm tested to say #2 makes the most sense, if it doesn't bloat things a ton.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Note that the tests also use the pythonsed package, not just pytest and pytest-xdist

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, have a draft PR up: zmkfirmware/zmk-docker#136 will inspect the images sizes to compare, the local builds w/ buildah here seemed just a tiny bit larger.

Replaced the run-test.sh script with a Python script that runs our unit
tests through pytest. Tests are now run in parallel to speed up running
the entire test suite, and it allows for integration with other tools
that support pytest, such as IDEs.

Also removed a dependency on remarshal, because it depends on an old
version of PyYAML that conflicts with other Python packages in our
Docker image. Replaced it with yq.
@joelspadin joelspadin marked this pull request as draft November 9, 2024 02:27
@github-actions
Copy link
Copy Markdown

github-actions bot commented Sep 5, 2025

This PR has been automatically marked as stale because it has not had activity in 10 months. It will be closed in 14 days if no further activity occurs. Feel free to give a status update or re-open when it has been rebased and is ready for review (again). Thanks!

@github-actions github-actions bot added the Stale label Sep 5, 2025
@github-actions
Copy link
Copy Markdown

This PR was closed because it had no activity for over 10 months. Feel free to give a status update or re-open when it has been rebased and is ready for review (again).

@github-actions github-actions bot closed this Sep 19, 2025
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