Skip to content

Commit

Permalink
examples: zephyr: location: add pytest test for native_sim
Browse files Browse the repository at this point in the history
Rely on playback data to result in accurate location data. Wait for first 5
locations and match logged data with expected position.

Signed-off-by: Marcin Niestroj <[email protected]>
  • Loading branch information
mniestroj committed Jan 11, 2025
1 parent c2ac22d commit 0eee67f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/zephyr/location/wifi/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pytest

@pytest.fixture(scope='session')
def anyio_backend():
return 'trio'
43 changes: 43 additions & 0 deletions examples/zephyr/location/wifi/pytest/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2025 Golioth, Inc.
#
# SPDX-License-Identifier: Apache-2.0

from collections import namedtuple
import re

from twister_harness.device.device_adapter import DeviceAdapter
from twister_harness.helpers.shell import Shell

import pytest


pytestmark = pytest.mark.anyio


async def test_location_wifi(shell: Shell, dut: DeviceAdapter, device):
# Set Golioth credential

golioth_cred = (await device.credentials.list())[0]
shell.exec_command(f"settings set golioth/psk-id {golioth_cred.identity}")
shell.exec_command(f"settings set golioth/psk {golioth_cred.key}")

# Wait for Golioth connection

dut.readlines_until(regex=".*Golioth CoAP client connected", timeout=90.0)

# Verify position

pattern = re.compile(r".* (?P<lon>\d+\.\d+) (?P<lat>\d+\.\d+) \((?P<acc>\d+)\)")
Position = namedtuple('Position', ('lon', 'lat', 'acc'))

positions = []
for _ in range(5):
lines = dut.readlines_until(regex=pattern, timeout=10.0)
m = pattern.search(lines[-1])
pos = Position(*[float(m[p]) for p in ['lon', 'lat', 'acc']])
positions.append(pos)

for pos in positions:
assert pos.lon == pytest.approx(50.663974800, 1e-3)
assert pos.lat == pytest.approx(17.942322850, 1e-3)
assert pos.acc < 1000
11 changes: 9 additions & 2 deletions examples/zephyr/location/wifi/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ sample:
description: Location WiFi
name: location wifi
common:
build_only: true
tags:
- golioth
- location
- socket
tests:
sample.golioth.location_wifi:
build_only: true
platform_allow:
- esp32_devkitc_wrover/esp32/procpu
- nrf52840dk/nrf52840
sample.golioth.location_wifi.playback:
harness: pytest
extra_args: EXTRA_CONF_FILE="../../common/runtime_settings.conf"
extra_configs:
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
platform_allow:
- native_sim
- native_sim/native/64
- nrf52840dk/nrf52840

0 comments on commit 0eee67f

Please sign in to comment.