pytest_gcov.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. import os.path
  4. import time
  5. import pytest
  6. from pytest_embedded_idf import IdfDut
  7. from pytest_embedded_jtag import OpenOcd
  8. @pytest.mark.esp32
  9. @pytest.mark.jtag
  10. @pytest.mark.parametrize(
  11. 'embedded_services, no_gdb',
  12. [
  13. ('esp,idf,jtag', 'y'),
  14. ],
  15. indirect=True,
  16. )
  17. def test_gcov(dut: IdfDut, openocd: OpenOcd) -> None:
  18. # create the generated .gcda folder, otherwise would have error: failed to open file.
  19. # normally this folder would be created via `idf.py build`. but in CI the non-related files would not be preserved
  20. os.makedirs(os.path.join(dut.app.binary_path, 'esp-idf', 'main', 'CMakeFiles', '__idf_main.dir'), exist_ok=True)
  21. os.makedirs(os.path.join(dut.app.binary_path, 'esp-idf', 'sample', 'CMakeFiles', '__idf_sample.dir'), exist_ok=True)
  22. def expect_counter_output(loop: int, timeout: int = 10) -> None:
  23. dut.expect_exact(
  24. [f'blink_dummy_func: Counter = {loop}', f'some_dummy_func: Counter = {loop * 2}'],
  25. expect_all=True,
  26. timeout=timeout,
  27. )
  28. expect_counter_output(0)
  29. dut.expect('Ready to dump GCOV data...', timeout=5)
  30. def dump_coverage(cmd: str) -> None:
  31. response = openocd.write(cmd)
  32. expect_lines = [
  33. 'Targets connected.',
  34. 'gcov_example_main.c.gcda',
  35. 'gcov_example_func.c.gcda',
  36. 'some_funcs.c.gcda',
  37. 'Targets disconnected.',
  38. ]
  39. for line in response.splitlines():
  40. for expect in expect_lines[:]:
  41. if expect in line:
  42. if expect.endswith('.gcda'): # check file exists
  43. file_path = line.split()[3].strip("'")
  44. assert os.path.isfile(file_path)
  45. expect_lines.remove(expect)
  46. assert len(expect_lines) == 0
  47. # Test two hard-coded dumps
  48. dump_coverage('esp gcov dump')
  49. dut.expect('GCOV data have been dumped.', timeout=5)
  50. expect_counter_output(1)
  51. dut.expect('Ready to dump GCOV data...', timeout=5)
  52. dump_coverage('esp gcov dump')
  53. dut.expect('GCOV data have been dumped.', timeout=5)
  54. for i in range(2, 6):
  55. expect_counter_output(i)
  56. for _ in range(3):
  57. time.sleep(1)
  58. # Test instant run-time dump
  59. dump_coverage('esp gcov')