|
16 | 16 | ZEPHYR_BASE = os.getenv("ZEPHYR_BASE") |
17 | 17 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister")) |
18 | 18 |
|
19 | | -from twisterlib.harness import Gtest, Bsim |
| 19 | +from twisterlib.harness import Gtest, Bsim, Cpputest |
20 | 20 | from twisterlib.harness import Harness |
21 | 21 | from twisterlib.harness import Robot |
22 | 22 | from twisterlib.harness import Test |
|
38 | 38 | "[00:00:00.000,000] [0m<inf> label: [==========] Done running all tests.[0m" |
39 | 39 | ) |
40 | 40 |
|
| 41 | +SAMPLE_CPPUTEST_NO_TESTS = ( |
| 42 | + "Errors (ran nothing, 0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 0 ms)") |
| 43 | +SAMPLE_CPPUTEST_START_FMT = "[00:00:00.000,000] [0m<inf> label: TEST({suite}, {test})[0m" |
| 44 | +SAMPLE_CPPUTEST_END_PASS_FMT = "[00:00:00.000,000] [0m<inf> label: OK ({tests} tests" \ |
| 45 | + ", {ran} ran, {checks} checks, {ignored} ignored," \ |
| 46 | + " {filtered} filtered out, {time} ms)[0m" |
| 47 | +SAMPLE_CPPUTEST_FAIL_FMT = "[00:00:00.000,000] [0m<inf> label: Failure in TEST({suite}, {test})[0m" |
| 48 | +SAMPLE_CPPUTEST_END_FAIL_FMT = "[00:00:00.000,000] [0m<inf> label: Errors({failures} failures" \ |
| 49 | + ", {tests} tests, {ran} ran, {checks} checks, {ignored} ignored," \ |
| 50 | + " {filtered} filtered out, {time} ms)[0m" |
| 51 | + |
41 | 52 |
|
42 | 53 | def process_logs(harness, logs): |
43 | 54 | for line in logs: |
@@ -795,6 +806,124 @@ def test_gtest_repeated_run(gtest): |
795 | 806 | ) |
796 | 807 |
|
797 | 808 |
|
| 809 | +@pytest.fixture |
| 810 | +def cpputest(tmp_path): |
| 811 | + mock_platform = mock.Mock() |
| 812 | + mock_platform.name = "mock_platform" |
| 813 | + mock_platform.normalized_name = "mock_platform" |
| 814 | + mock_testsuite = mock.Mock() |
| 815 | + mock_testsuite.name = "mock_testsuite" |
| 816 | + mock_testsuite.detailed_test_id = True |
| 817 | + mock_testsuite.id = "id" |
| 818 | + mock_testsuite.testcases = [] |
| 819 | + mock_testsuite.harness_config = {} |
| 820 | + outdir = tmp_path / 'cpputest_out' |
| 821 | + outdir.mkdir() |
| 822 | + |
| 823 | + instance = TestInstance(testsuite=mock_testsuite, platform=mock_platform, outdir=outdir) |
| 824 | + |
| 825 | + harness = Cpputest() |
| 826 | + harness.configure(instance) |
| 827 | + return harness |
| 828 | + |
| 829 | + |
| 830 | +def test_cpputest_start_test_no_suites_detected(cpputest): |
| 831 | + process_logs(cpputest, [SAMPLE_CPPUTEST_NO_TESTS]) |
| 832 | + assert len(cpputest.detected_suite_names) == 0 |
| 833 | + assert cpputest.state is None |
| 834 | + |
| 835 | + |
| 836 | +def test_cpputest_start_test(cpputest): |
| 837 | + process_logs( |
| 838 | + cpputest, |
| 839 | + [ |
| 840 | + SAMPLE_CPPUTEST_START_FMT.format( |
| 841 | + suite="suite_name", test="test_name" |
| 842 | + ), |
| 843 | + ], |
| 844 | + ) |
| 845 | + assert cpputest.state is None |
| 846 | + assert len(cpputest.detected_suite_names) == 1 |
| 847 | + assert cpputest.detected_suite_names[0] == "suite_name" |
| 848 | + assert cpputest.instance.get_case_by_name("id.suite_name.test_name") is not None |
| 849 | + assert ( |
| 850 | + cpputest.instance.get_case_by_name("id.suite_name.test_name").status == "started" |
| 851 | + ) |
| 852 | + |
| 853 | + |
| 854 | +def test_cpputest_one_test_passed(cpputest): |
| 855 | + process_logs( |
| 856 | + cpputest, |
| 857 | + [ |
| 858 | + SAMPLE_CPPUTEST_START_FMT.format( |
| 859 | + suite="suite_name", test="test_name" |
| 860 | + ), |
| 861 | + SAMPLE_CPPUTEST_END_PASS_FMT.format( |
| 862 | + tests=1, ran=1, checks=5, ignored=0, filtered=0, time=10 |
| 863 | + ) |
| 864 | + ], |
| 865 | + ) |
| 866 | + assert len(cpputest.detected_suite_names) == 1 |
| 867 | + assert cpputest.detected_suite_names[0] == "suite_name" |
| 868 | + assert cpputest.instance.get_case_by_name("id.suite_name.test_name") is not None |
| 869 | + assert cpputest.instance.get_case_by_name("id.suite_name.test_name").status == "passed" |
| 870 | + |
| 871 | + |
| 872 | +def test_cpputest_multiple_test_passed(cpputest): |
| 873 | + logs = [] |
| 874 | + total_passed_tests = 5 |
| 875 | + for i in range(0, total_passed_tests): |
| 876 | + logs.append(SAMPLE_CPPUTEST_START_FMT.format(suite="suite_name", |
| 877 | + test="test_name_%d" % i)) |
| 878 | + logs.append(SAMPLE_CPPUTEST_END_PASS_FMT.format( |
| 879 | + tests=total_passed_tests, ran=total_passed_tests, checks=5, ignored=0, filtered=0, time=10 |
| 880 | + )) |
| 881 | + process_logs(cpputest, logs) |
| 882 | + assert len(cpputest.detected_suite_names) == 1 |
| 883 | + assert cpputest.detected_suite_names[0] == "suite_name" |
| 884 | + for i in range(0, total_passed_tests): |
| 885 | + test_name = "id.suite_name.test_name_%d" % i |
| 886 | + assert cpputest.instance.get_case_by_name(test_name) is not None |
| 887 | + assert cpputest.instance.get_case_by_name(test_name).status == "passed" |
| 888 | + |
| 889 | + |
| 890 | +def test_cpputest_test_failed(cpputest): |
| 891 | + process_logs( |
| 892 | + cpputest, |
| 893 | + [ |
| 894 | + SAMPLE_CPPUTEST_START_FMT.format( |
| 895 | + suite="suite_name", test="test_name" |
| 896 | + ), |
| 897 | + SAMPLE_CPPUTEST_FAIL_FMT.format( |
| 898 | + suite="suite_name", test="test_name" |
| 899 | + ) |
| 900 | + ], |
| 901 | + ) |
| 902 | + assert cpputest.state is None |
| 903 | + assert len(cpputest.detected_suite_names) == 1 |
| 904 | + assert cpputest.detected_suite_names[0] == "suite_name" |
| 905 | + assert cpputest.instance.get_case_by_name("id.suite_name.test_name") is not None |
| 906 | + assert cpputest.instance.get_case_by_name("id.suite_name.test_name").status == "failed" |
| 907 | + |
| 908 | + |
| 909 | +def test_cpputest_test_repeated(cpputest): |
| 910 | + with pytest.raises( |
| 911 | + AssertionError, |
| 912 | + match=r"CppUTest error, id.suite_name.test_name running twice", |
| 913 | + ): |
| 914 | + process_logs( |
| 915 | + cpputest, |
| 916 | + [ |
| 917 | + SAMPLE_CPPUTEST_START_FMT.format( |
| 918 | + suite="suite_name", test="test_name" |
| 919 | + ), |
| 920 | + SAMPLE_CPPUTEST_START_FMT.format( |
| 921 | + suite="suite_name", test="test_name" |
| 922 | + ), |
| 923 | + ], |
| 924 | + ) |
| 925 | + |
| 926 | + |
798 | 927 | def test_bsim_build(monkeypatch, tmp_path): |
799 | 928 | mocked_instance = mock.Mock() |
800 | 929 | build_dir = tmp_path / 'build_dir' |
|
0 commit comments