Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitycheck: Add a feature which can handle pytest script. #29427

Closed

Conversation

youhuazhu
Copy link
Collaborator

@youhuazhu youhuazhu commented Oct 22, 2020

  1. Add a class of Pytest(Harness) which can handle pytest script
    in harness.py
  2. Use running_dir to store current test directory which be
    used in pytest scrpt.
  3. Add a new section(handle_script) which used to specify the pytest
    script in yaml file.
    Add an example in tests/pytest_sample/

@github-actions github-actions bot added the area: Sanitycheck Sanitycheck has been renamed to Twister label Oct 22, 2020
@youhuazhu youhuazhu force-pushed the sanitycheck_pytest branch 2 times, most recently from aeaf374 to 607e95c Compare October 22, 2020 02:15
@galak
Copy link
Collaborator

galak commented Oct 22, 2020

What's the use case for this?

@youhuazhu
Copy link
Collaborator Author

What's the use case for this?

In same case, we need to excute external script for check the zephyr output data, for example, We need to convert the tracing CTF data to text format and check if it contain thread/semaphore ....
so as Anas's suggestion, I add this feature into sanitycheck for common use.

@LeiW000 LeiW000 requested a review from galak October 22, 2020 05:46
@LeiW000
Copy link
Collaborator

LeiW000 commented Oct 22, 2020

@nashif please help to take look at it.

@nashif
Copy link
Member

nashif commented Oct 22, 2020

What's the use case for this?

for post-run/post-build tests, like examining recorded output (tracing), running tests on generated output and artifacts etc.

@nashif nashif requested a review from PerMac October 22, 2020 12:02
scripts/sanity_chk/harness.py Outdated Show resolved Hide resolved
doc/guides/test/sanitycheck.rst Outdated Show resolved Hide resolved
scripts/sanity_chk/harness.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the area: Tests Issues related to a particular existing or missing test label Oct 27, 2020
@youhuazhu youhuazhu force-pushed the sanitycheck_pytest branch 2 times, most recently from edd2ba3 to aa90791 Compare October 27, 2020 08:07
@youhuazhu
Copy link
Collaborator Author

Hi @nashif , I have updated this PR accourding your comments, would you help to review it again?

@LeiW000
Copy link
Collaborator

LeiW000 commented Nov 5, 2020

@nashif , could you take look at it again? Youhua had updated the code.

::

tests:
test:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.zephyrproject.org/latest/guides/test/sanitycheck.html#test-cases:

Each test identifier shall start with a section followed by a subsection separated by a dot. For example, a test that covers semaphores in the kernel shall start with kernel.sempahore.

You can use pytest.example in here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #33227

test:
harness: pytest
harness_config:
pytest_root: [pytest directory name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you added the option for specifying an individual timeout for a pytest test, it would be beneficial if you add its usage in this example.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout removed in #33227

self.running_dir = config.get('running_dir', None)
self.source_dir = config.get('source_dir', None)
self.pytest_root = config.get('pytest_root', 'pytest')
self.pytest_timeout = config.get('pytest_timeout', 'pytest')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy-paste error? -> config.get('pytest_timeout', 15) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.
And the example commit is included in this PR

scripts/sanity_chk/harness.py Outdated Show resolved Hide resolved
@youhuazhu youhuazhu force-pushed the sanitycheck_pytest branch 6 times, most recently from 59589c6 to 12751d9 Compare December 14, 2020 13:07
Copy link
Member

@PerMac PerMac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I am not sure if the location of the pytest example is correct within the zephyr tree. In zephyr/tests/ directories correspond to different modules. I think it will be better if pytest_sample is one level deeper, eg. zephyr/tests/pytest/pytest_sample @nashif Any thoughts on this?

  • The output of pytest is quite verbose. By default, you should capture the stdout of pytest when called with subprocess and only show it when sanitycheck is called with -v -v

  • There is something wrong going on when I try this pytest example. This is the output I am getting when calling: ./scripts/twister -T tests/pytest_sample/:

show output

$ ./scripts/twister -T tests/pytest_sample/
ZEPHYR_BASE unset, using "/home/maciej/zephyrproject2/zephyr"
Renaming output directory to /home/maciej/zephyrproject2/zephyr/twister-out.42
INFO - Zephyr version: zephyr-v2.4.0-2373-g12751d994e
INFO - JOBS: 4
INFO - Selecting default platforms per test case
INFO - Building initial testcase list...
INFO - 1 test suites (2 configurations) selected, 22 configurations discarded due to filters.
INFO - Adding tasks to the queue...
INFO - Added initial list of jobs to queue
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample/report.html ----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample/report.html ----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.07s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample/report.html ----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.04s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample/report.html ----------------------------
=========================================================================== 1 passed, 1 warning in 0.41s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
.================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample/report.html ----------------------------
=========================================================================== 1 passed, 1 warning in 0.50s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86_64/tests/pytest_sample/pytest.sample/report.html ----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
INFO - Total complete: 1/ 2 50% skipped: 0, failed: 0================================================================================ test session starts ================================================================================
platform linux -- Python 3.7.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/maciej/zephyrproject2/zephyr
collected 1 item

tests/pytest_sample/pytest/test_ctf.py /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
/home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample
.

================================================================================= warnings summary ==================================================================================
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446
/home/maciej/.pyenv/versions/zephyr377/lib/python3.7/site-packages/_pytest/junitxml.py:446: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0. See:
https://docs.pytest.org/en/stable/deprecations.html#junit-family-default-value-change-to-xunit2
for more information.
_issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
----------------------------- generated xml file: /home/maciej/zephyrproject2/zephyr/twister-out/qemu_x86/tests/pytest_sample/pytest.sample/report.html -----------------------------
=========================================================================== 1 passed, 1 warning in 0.01s ============================================================================
INFO - Total complete: 2/ 2 100% skipped: 0, failed: 0
INFO - 2 of 2 test configurations passed (100.00%), 0 failed, 0 skipped with 0 warnings in 7.67 seconds
INFO - In total 2 test cases were executed, 0 skipped on 2 out of total 313 platforms (0.64%)
INFO - 2 test configurations executed on platforms, 0 test configurations were only built.
INFO - Saving reports...
INFO - Writing xunit report /home/maciej/zephyrproject2/zephyr/twister-out/twister.xml...
INFO - Writing xunit report /home/maciej/zephyrproject2/zephyr/twister-out/twister_report.xml...
INFO - Writing JSON report /home/maciej/zephyrproject2/zephyr/twister-out/twister.json
INFO - Run completed

It seems that the same tests are called multiple times. I tried debugging and I think the thread where harness.handle(line) is going on is called too many times (line 988 in twisterlib.py). I belive it is some harness design issue, as from my understanding, there is no real line checking going on in this place. Other harnesses are checking lines from output if they match some given pattern. In pytest harness, the procedure not only check the lines in the output but call pytest with subprocess each time it happens. This behavior requires redesign. @nashif any hints on how this can be done?

  • It also looks like a weird design that pytest test requires now the full structure of standard tests in c, i.e. having in main.c
void test_pytest(void)
{
	TC_PRINT("Hello world\n");
}

where in the end the actual test is in test_ctf.py and have nothing to do with whatever is going on in main.c:

def test_case(cmdopt):
    ''' test case '''
    if not os.path.exists(cmdopt):
        assert 0
    print(cmdopt)

I guess that this is some simple approach which doesn't require changing of the default mode, where ztest structures are used. I will accept this approach, but it is pretty confusing. This is also something which would require your attention @nashif

@@ -369,6 +369,11 @@ harness_config: <harness configuration options>

Only one fixture can be defined per testcase.

handle_script: <pytest dirctory> (default pytest)
Specify a pytest directory which need to excute when test case begin to running,
default pytest directory name is pytest, after pytest finished, sanitycheck will
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sanitycheck -> twister

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated. refine the output of pytest

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PerMac Can you check it? thank you!

@zephyrbot zephyrbot added the area: Twister Twister label Jan 8, 2021
@zephyrbot zephyrbot requested a review from nashif January 8, 2021 13:28
@nashif nashif removed the area: Sanitycheck Sanitycheck has been renamed to Twister label Jan 11, 2021
@youhuazhu youhuazhu force-pushed the sanitycheck_pytest branch 3 times, most recently from ebb7c38 to ff2c862 Compare January 15, 2021 07:53
1. Add a class of Pytest(Harness) which can handle pytest script
in harness.py
2. Use running_dir to store current test directory which be
used in pytest scrpt.
3. Add a new section(handle_script) which used to specify the pytest
script in yaml file.
4. Add usage of this feature into zephyr doc.

Signed-off-by: YouhuaX Zhu <youhuax.zhu@intel.com>
In this example, python test case get the running directory by handling
the "--cmdopt" option passed by pytest.

Signed-off-by: YouhuaX Zhu <youhuax.zhu@intel.com>
@youhuazhu youhuazhu force-pushed the sanitycheck_pytest branch 2 times, most recently from 762ecb5 to f0af9b4 Compare January 15, 2021 08:09
@LeiW000
Copy link
Collaborator

LeiW000 commented Mar 1, 2021

@PerMac @nashif, your comments have been addressed. Could you take look at the PR again? The PR has been there for a few months.

'-q'
]

with subprocess.Popen(cmd) as proc:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will generate lots of noise

i9:pytest_sample((HEAD detached from upstream/pull/29427)): twister -p qemu_x86 -T .
INFO    - Zephyr version: zephyr-v2.5.0-838-gba4a8fc61df2
INFO    - JOBS: 20
INFO    - Using 'zephyr' toolchain.
INFO    - Building initial testcase list...
INFO    - 1 test scenarios (1 configurations) selected, 0 configurations discarded due to filters.
INFO    - Adding tasks to the queue...
INFO    - Added initial list of jobs to queue
handle cmdopt:
/home/nashif/Work/zephyrproject/zephyr/tests/pytest/pytest_sample/twister-out/qemu_x86/tests/pytest/pytest_sample/pytest.sample
run test cases in:
/home/nashif/Work/zephyrproject/zephyr/tests/pytest/pytest_sample/twister-out/qemu_x86/tests/pytest/pytest_sample/pytest.sample
.
- generated xml file: /home/nashif/Work/zephyrproject/zephyr/tests/pytest/pytest_sample/twister-out/qemu_x86/tests/pytest/pytest_sample/pytest.sample/report.html -
1 passed in 0.01s
INFO    - Total complete:    1/   1  100%  skipped:    0, failed:    0
INFO    - 1 of 1 test configurations passed (100.00%), 0 failed, 0 skipped with 0 warnings in 19.91 seconds
INFO    - In total 1 test cases were executed, 0 skipped on 1 out of total 330 platforms (0.30%)
INFO    - 1 test configurations executed on platforms, 0 test configurations were only built.
INFO    - Saving reports...
INFO    - Writing xunit report /home/nashif/Work/zephyrproject/zephyr/tests/pytest/pytest_sample/twister-out/twister.xml...
INFO    - Writing xunit report /home/nashif/Work/zephyrproject/zephyr/tests/pytest/pytest_sample/twister-out/twister_report.xml...
INFO    - Run completed

We do not want to see pytest intermediate results in the twister output.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #33227

'--cmdopt',
self.running_dir,
'--junit-xml',
os.path.join(self.running_dir, 'report.html'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

report.xml

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #33227

@@ -41,6 +49,10 @@ def configure(self, instance):
self.repeat = config.get('repeat', 1)
self.ordered = config.get('ordered', True)
self.record = config.get('record', {})
self.running_dir = config.get('running_dir', None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we defining all of this, we know where we are running and where the source directory is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #33227

self.running_dir = config.get('running_dir', None)
self.source_dir = config.get('source_dir', None)
self.pytest_root = config.get('pytest_root', 'pytest')
self.pytest_timeout = config.get('pytest_timeout', 15)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why introducing this new timeout, the test scenario already has a timeout.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in #33227

"pytest_root":
type: str
required: no
"pytest_timeout":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for a new timeout, use the existing one

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -0,0 +1,24 @@
# Copyright (c) 2020 Intel Corporation.
#
# SPDX-License-Identifier: Apache-2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_ctf? need another file name.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #33227

@@ -0,0 +1,4 @@
tests:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this under tests/testsuite/pytest

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't push to youhua's repo any more, I update this PR to #33227

Copy link
Member

@PerMac PerMac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments added. In general, I find the workflow for pytest test convoluted and the implementation to be suboptimal.

pytest handle output as a whole, it's not necessary to handle it
line by line
'''
global pytest_running_dir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global? AFAIK it is highly advised not to use global variables as this can lead to a lot of mess in the software and problems with debugging. It seems you even never use it anywhere else, so why the global here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a variable to remember pytest has been called for this case, so I use pytest_running_dir to identify this directory has been visited then pytest has been called. In C I could use "static" variable but in python I couldn't find an equivalence, so I use "global pytest_running_dir"

Comment on lines +35 to +38
self.running_dir = None
self.source_dir = None
self.pytest_root = 'pytest'
self.pytest_timeout = 15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are not needed by default, so why initializing them here? Your class Pytest inherits from Harness. You can overcharge the __init__ method in Pytest by calling the parent __init__ and adding extra stuff you need. This will help in keeping the modularity and coherence of the code
https://appdividend.com/2019/01/22/python-super-function-example-super-method-tutorial/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227, move to pytest.configure()

Comment on lines +52 to +55
self.running_dir = config.get('running_dir', None)
self.source_dir = config.get('source_dir', None)
self.pytest_root = config.get('pytest_root', 'pytest')
self.pytest_timeout = config.get('pytest_timeout', 15)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above. You can overwrite the configure() method in Pytest class. You can see below, in Console(Harness) how this is done there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227, move to pytest.configure()

'''
global pytest_running_dir
if self.running_dir is pytest_running_dir:
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what this logic suppose to do. Why would you return like that just at the beginning of handle()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure this handle be called only once. Every line output from console will cause this handle be called. I need a variable to remember pytest has been called for this case, so I use pytest_running_dir to identify this directory has been visited then pytest has been called. In C I could use "static" variable but in python I couldn't find an equivalence, so I use
"global pytest_running_dir"

Comment on lines +1 to +19
/*
* Copyright (c) 2020 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
void test_pytest(void)
{
TC_PRINT("Hello world\n");
}

void test_main(void)
{
ztest_test_suite(test_pytest,
ztest_unit_test(test_pytest)
);

ztest_run_test_suite(test_pytest);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nashif Were you able to follow the design of this "feature"? It builds and executes some hello_word sample, which is in fact not tested. It is just a "workaround" to fit the existing structure of a workflow, where something has to be built. Then the sample is executed and the output line is "handled". In fact, the line is not checked but serves as a trigger for calling pytest. The test for pytest has nothing to do with the building sample.

'-q'
]

with subprocess.Popen(cmd) as proc:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should pipe the stdout and stderr so the messages from tests are not printed to the output when not needed (no verbose used)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227

Comment on lines +512 to +513
harness.running_dir = self.build_dir
harness.source_dir = self.sourcedir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you've added this to all already existing handlers, where it is not needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227, move to pytest.configure()

Comment on lines +794 to +795
harness.running_dir = self.build_dir
harness.source_dir = self.sourcedir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you've added this to all already existing handlers, where it is not needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227, move to pytest.configure()

Comment on lines +1060 to +1061
harness.running_dir = self.build_dir
harness.source_dir = self.sourcedir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you've added this to all already existing handlers, where it is not needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227

@@ -0,0 +1,4 @@
tests:
pytest.sample:
platform_allow: qemu_x86 qemu_x86_64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes the same test to be run 2x. As I understand, the platform chosen has no impact on the actual test. It just serves as a stub so sth is build and handling lines can trigger calling pytest. I would say that there should be just a single "native_posix" if we accept this workflow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #33227

Comment on lines +18 to +19
if not os.path.exists(cmdopt):
assert 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just:

assert os.path.exists(cmdopt)

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I've updated it in #33227

@nashif
Copy link
Member

nashif commented Mar 8, 2021

a

Comments added. In general, I find the workflow for pytest test convoluted and the implementation to be suboptimal.

agree on the implementation, but it seems the purpose of this is being missed here.

The idea behind adding this mode is to be able to use pytest in this case to do post run tests on build collateral and build output, for example, some subsystems generate binary data over UART (tracing) that could be evaluated after the test is run using pytest or we could be do post build analysis on generated binaries and linker scripts, something you can't do using current ztest.

Ideally we want to merge the results of pyests in the report as additional testcases, this is not being done here but we have a path to get this in after the restructuring of test results is complete.

@PerMac
Copy link
Member

PerMac commented Mar 9, 2021

agree on the implementation, but it seems the purpose of this is being missed here.

@nashif I am not sure who you address here :) By convoluted I meant how the workflow is done in the PR:

  • some phony sample is built just to trigger output handler which calls for pytest. The sample built has nothing to do with pytest

@nashif
Copy link
Member

nashif commented Mar 9, 2021

* some phony sample is built just to trigger output handler which calls for pytest. The sample built has nothing to do with pytest

the sample can be anything if you for example checking the output and verifying that for example files are being produced in some format you expect. I would agree that the sample should show some real use case and not just call some useless asserts.

@mengxianglinx mengxianglinx mentioned this pull request Mar 11, 2021
@LeiW000
Copy link
Collaborator

LeiW000 commented Mar 16, 2021

@mengxianglinx, do you need to close this PR as #33227 has been filed.

The background on why Xianglin had to file #33227: @youhuazhu, who is the owner of this PR, does not work on the Zephyr project anymore. Xianglin is not able to update this PR with his account. So he has filed #33227.

@nashif
Copy link
Member

nashif commented Apr 19, 2021

obsoleted by #33227

@nashif nashif closed this Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Documentation area: Tests Issues related to a particular existing or missing test area: Twister Twister
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants