Skip to content

Commit

Permalink
esp-idf: cpp: add pytest
Browse files Browse the repository at this point in the history
Add pytest for automated testing of the cpp sample.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Jan 7, 2025
1 parent 092250f commit 1a4e047
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/esp_idf/cpp/pytest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CPP Test

Compile an application in CPP and confirm it successfully connects to
Golioth.

See esp_idf/README.md for test requirements and commands to run the
test.
9 changes: 9 additions & 0 deletions examples/esp_idf/cpp/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024 Golioth, Inc.
#
# SPDX-License-Identifier: Apache-2.0

import pytest

@pytest.fixture(scope='session')
def anyio_backend():
return 'trio'
24 changes: 24 additions & 0 deletions examples/esp_idf/cpp/pytest/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
import pytest
from datetime import datetime, timezone
import re

pytestmark = pytest.mark.anyio

async def test_hello(board, device):
# Set Golioth credential
golioth_cred = (await device.credentials.list())[0]
await board.set_golioth_psk_credentials(golioth_cred.identity, golioth_cred.key)

# Record timestamp and wait for fourth hello message
start = datetime.now(timezone.utc)
await board.wait_for_regex_in_line('.*Hello, Golioth 2!', timeout_s=90.0)

# Check logs for hello messages
end = datetime.now(timezone.utc)
logs = await device.get_logs({'start': start.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'end': end.strftime('%Y-%m-%dT%H:%M:%S.%fZ')})

# Test logs received from server
r = re.compile(".*Hello, Golioth 1!")
matching_log = list(filter(r.match, [str(l) for l in logs]))
assert len(matching_log) == 1

0 comments on commit 1a4e047

Please sign in to comment.