Skip to content

Commit d222623

Browse files
committed
examples: zephyr: location: add pytest test for native_sim
Rely on playback data to result in accurate location data. Wait for first 3 locations and match logged data with expected position. Signed-off-by: Marcin Niestroj <[email protected]>
1 parent 8e3d441 commit d222623

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 Golioth, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import pytest
6+
7+
@pytest.fixture(scope='session')
8+
def anyio_backend():
9+
return 'trio'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2025 Golioth, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from collections import namedtuple
6+
import re
7+
8+
from twister_harness.device.device_adapter import DeviceAdapter
9+
from twister_harness.helpers.shell import Shell
10+
11+
import pytest
12+
13+
14+
pytestmark = pytest.mark.anyio
15+
16+
17+
async def test_location(shell: Shell, dut: DeviceAdapter, device):
18+
# Set Golioth credential
19+
20+
golioth_cred = (await device.credentials.list())[0]
21+
shell.exec_command(f"settings set golioth/psk-id {golioth_cred.identity}")
22+
shell.exec_command(f"settings set golioth/psk {golioth_cred.key}")
23+
24+
# Wait for Golioth connection
25+
26+
dut.readlines_until(regex=".*Golioth CoAP client connected", timeout=90.0)
27+
28+
# Verify position
29+
30+
pattern = re.compile(r".* (?P<lon>\d+\.\d+) (?P<lat>\d+\.\d+) \((?P<acc>\d+)\)")
31+
Position = namedtuple('Position', ('lon', 'lat', 'acc'))
32+
33+
positions = []
34+
for _ in range(3):
35+
lines = dut.readlines_until(regex=pattern, timeout=30.0)
36+
m = pattern.search(lines[-1])
37+
pos = Position(*[float(m[p]) for p in ['lon', 'lat', 'acc']])
38+
positions.append(pos)
39+
40+
for pos in positions:
41+
assert pos.lon == pytest.approx(50.663974800, 1e-3)
42+
assert pos.lat == pytest.approx(17.942322850, 1e-3)
43+
assert pos.acc < 2000

examples/zephyr/location/sample.yaml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,46 @@ sample:
22
description: Location example
33
name: location
44
common:
5-
build_only: true
65
tags:
76
- golioth
87
- location
98
- socket
109
tests:
1110
sample.golioth.location:
11+
build_only: true
1212
platform_allow:
1313
- esp32_devkitc_wrover/esp32/procpu
1414
- native_sim
1515
- native_sim/native/64
1616
- nrf52840dk/nrf52840
1717
- nrf9151dk/nrf9151/ns
1818
- nrf9160dk/nrf9160/ns
19-
sample.golioth.location.wifi_only:
19+
sample.golioth.location.playback:
20+
timeout: 100
21+
harness: pytest
22+
extra_args: EXTRA_CONF_FILE="../common/runtime_settings.conf"
2023
extra_configs:
24+
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
25+
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
26+
platform_allow:
27+
- native_sim
28+
- native_sim/native/64
29+
sample.golioth.location.playback.wifi:
30+
harness: pytest
31+
extra_args: EXTRA_CONF_FILE="../common/runtime_settings.conf"
32+
extra_configs:
33+
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
34+
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
2135
- CONFIG_GOLIOTH_CELLULAR_PLAYBACK=n
2236
platform_allow:
2337
- native_sim
2438
- native_sim/native/64
25-
sample.golioth.location.cellular_only:
39+
sample.golioth.location.playback.cellular:
40+
harness: pytest
41+
extra_args: EXTRA_CONF_FILE="../common/runtime_settings.conf"
2642
extra_configs:
43+
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
44+
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
2745
- CONFIG_GOLIOTH_WIFI_PLAYBACK=n
2846
platform_allow:
2947
- native_sim

0 commit comments

Comments
 (0)