Skip to content

Commit d9ae1b7

Browse files
committed
Add GitHub Actions workflow for unit tests on PRs
Added workflow to run unit tests and doctests on pull requests targeting main or dev branches, using Python 3.8 container. The workflow: - Runs on ubuntu-latest with python:3.8 container - Installs soco and automate-home dependencies - Runs unittest discovery for all test files - Runs doctests on key modules (gateway, message, commands) Assisted-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 83b5663 commit d9ae1b7

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

.github/workflows/tests.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Unit Tests
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
- dev
8+
push:
9+
branches:
10+
- main
11+
- dev
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
container:
17+
image: python:3.8
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Install dependencies
24+
run: |
25+
pip install --upgrade pip
26+
pip install soco==0.24.0
27+
pip install git+https://github.com/majamassarini/automate-home.git
28+
29+
- name: Run unit tests
30+
run: |
31+
python -m unittest discover -v
32+
33+
- name: Run doctests
34+
run: |
35+
python -m doctest soco_plugin/gateway.py -v
36+
python -m doctest soco_plugin/message.py -v
37+
python -m doctest soco_plugin/command/play.py -v
38+
python -m doctest soco_plugin/command/pause.py -v
39+
python -m doctest soco_plugin/command/stop.py -v

soco_plugin/gateway.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def get_action(player: soco.SoCo, msg: "soco_plugin.message.Command") -> Callabl
159159
>>> import home
160160
>>> import soco_plugin
161161
>>> class Player:
162+
... player_name = "TestPlayer"
162163
... def play(self):
163164
... print("play")
164165
... def pause(self):
@@ -179,7 +180,12 @@ def get_action(player: soco.SoCo, msg: "soco_plugin.message.Command") -> Callabl
179180
... print("play mode is {}".format(mode))
180181
... def get_sonos_playlist_by_attr(self, attr, title):
181182
... print ("playlist title is {}".format(title))
182-
... return {"uri": "a uri"}
183+
... class Playlist:
184+
... def __init__(self):
185+
... class Resource:
186+
... uri = "a uri"
187+
... self.resources = [Resource()]
188+
... return Playlist()
183189
... def clear_queue(self):
184190
... pass
185191
... def add_uri_to_queue(self, uri):

soco_plugin/tests/test_gateway.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ async def postpone_gw_running(self):
2929

3030
async def asyncSetUp(self):
3131
self._gw = soco_plugin.Gateway()
32+
# Make _players dict support iteration for logging
33+
self._gw._players = {}
3234
self._loop = asyncio.get_event_loop()
3335
self._loop.create_task(
3436
self._gw.associate_triggers(
@@ -54,6 +56,7 @@ async def test_stopped(self):
5456

5557
test = Test("test_stopped")
5658
mock = unittest.mock.Mock()
59+
mock.player_name = "Bagno"
5760
event_mock = unittest.mock.Mock()
5861
event_mock.variables = {"transport_state": "STOPPED"}
5962
av_mock = unittest.mock.Mock()
@@ -118,6 +121,7 @@ async def test_stopped(self):
118121

119122
test = Test("test_stopped")
120123
mock = unittest.mock.Mock()
124+
mock.player_name = "Bagno"
121125
event_mock = unittest.mock.Mock()
122126
event_mock.variables = {"transport_state": "STOPPED"}
123127
av_mock = unittest.mock.Mock()

0 commit comments

Comments
 (0)