ztest: Add initial zexpect API for delayed failing

Add the zexpect API, inspired by GoogleTest's EXPECT API. This API reports
test failures while allowing test execution to continue. This enables test
reports to show more than a singule failing property on a failing test.

Signed-off-by: Aaron Massey <aaronmassey@google.com>
This commit is contained in:
Aaron Massey 2022-12-13 14:12:57 -07:00 committed by Carles Cufí
commit 107cb86bb3
8 changed files with 459 additions and 1 deletions

View file

@ -0,0 +1,21 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
if(BOARD STREQUAL unit_testing)
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(base)
target_sources(testbinary PRIVATE src/main.c)
else()
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(base)
if(CONFIG_CPLUSPLUS)
message(STATUS "adding main.cpp")
target_sources(app PRIVATE src/main.cpp)
else()
target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_USERSPACE app PRIVATE src/main_userspace.c)
endif()
endif()

View file

@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y

View file

@ -0,0 +1,163 @@
/*
* Copyright (c) 2022 Google Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <zephyr/ztest.h>
ZTEST_SUITE(expect, NULL, NULL, NULL, NULL, NULL);
ZTEST_EXPECT_FAIL(expect, test_fail_later);
ZTEST(expect, test_fail_later)
{
void *empty_ptr = NULL;
uint32_t val = 5;
zexpect_equal(val, 2);
zexpect_not_equal(val, 5);
zexpect_not_null(empty_ptr);
zassert_true(true);
}
ZTEST(expect, test_pass_expect_true)
{
zexpect_true(true);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_true);
ZTEST(expect, test_fail_expect_true)
{
zexpect_true(false);
}
ZTEST(expect, test_expect_false)
{
zexpect_false(false);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_false);
ZTEST(expect, test_fail_expect_false)
{
zexpect_false(true);
}
ZTEST(expect, test_expect_ok)
{
zexpect_ok(0);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_ok);
ZTEST(expect, test_fail_expect_ok)
{
zexpect_ok(5);
}
ZTEST(expect, test_expect_is_null)
{
void *ptr = NULL;
zexpect_is_null(ptr);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_is_null);
ZTEST(expect, test_fail_expect_is_null)
{
void *ptr = (void *)0x32137899;
zexpect_is_null(ptr);
}
ZTEST(expect, test_expect_not_null)
{
void *ptr = (void *)0x91517141;
zexpect_not_null(ptr);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_null);
ZTEST(expect, test_fail_expect_not_null)
{
zexpect_not_null(NULL);
}
ZTEST(expect, test_expect_equal)
{
zexpect_equal(5, 5);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_equal);
ZTEST(expect, test_fail_expect_equal)
{
zexpect_equal(5, 1);
}
ZTEST(expect, test_expect_not_equal)
{
zexpect_not_equal(5, 1);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_equal);
ZTEST(expect, test_fail_expect_not_equal)
{
zexpect_not_equal(5, 5);
}
ZTEST(expect, test_expect_equal_ptr)
{
int v = 9;
int *a = &v;
int *b = &v;
zexpect_equal_ptr(a, b);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_equal_ptr);
ZTEST(expect, test_fail_expect_equal_ptr)
{
int v = 9;
int *a = &v;
int *b = NULL;
zexpect_equal_ptr(a, b);
}
ZTEST(expect, test_expect_within)
{
zexpect_within(7, 5, 2);
zexpect_within(7, 7, 0);
zexpect_within(7, 7, 3);
zexpect_within(7, 7 + 3, 3);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_within);
ZTEST(expect, test_fail_expect_within)
{
zexpect_within(7, 5, 1);
}
ZTEST(expect, test_expect_between_inclusive)
{
zexpect_between_inclusive(-5, -10, 0);
zexpect_between_inclusive(5, 0, 10);
zexpect_between_inclusive(0, 0, 10);
zexpect_between_inclusive(10, 0, 10);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_between_inclusive);
ZTEST(expect, test_fail_expect_between_inclusive)
{
zexpect_between_inclusive(-50, -20, 30);
zexpect_between_inclusive(5, 6, 10);
zexpect_between_inclusive(5, 0, 4);
zexpect_between_inclusive(5, 0, 4);
zexpect_between_inclusive(5, 6, 10);
}

View file

@ -0,0 +1 @@
main.c

View file

@ -0,0 +1,19 @@
# Copyright (c) 2022 Google Inc
# SPDX-License-Identifier: Apache-2.0
common:
timeout: 15
integration_platforms:
- native_posix
tests:
testing.ztest.expect:
integration_platforms:
- native_posix
testing.ztest.expect_cpp:
extra_configs:
- CONFIG_CPLUSPLUS=y
- CONFIG_LIB_CPLUSPLUS=y
integration_platforms:
- native_posix
testing.ztest.expect.unit:
type: unit