diff mbox series

[v2] unit-tests: show location of checks outside of tests

Message ID d92e5854-e916-49eb-a79e-9dc43778a7ea@web.de (mailing list archive)
State New
Headers show
Series [v2] unit-tests: show location of checks outside of tests | expand

Commit Message

René Scharfe July 23, 2024, 6:02 a.m. UTC
Checks outside of tests are caught at runtime and reported like this:

 Assertion failed: (ctx.running), function test_assert, file test-lib.c, line 267.

The assert() call aborts the unit test and doesn't reveal the location
or even the type of the offending check, as test_assert() is called by
all of them.

Handle it like the opposite case, a test without any checks: Don't
abort, but report the location of the actual check, along with a message
explaining the situation.  The output for example above becomes:

 # BUG: check outside of test at t/helper/test-example-tap.c:75

... and the unit test program continues and indicates the error in its
exit code at the end.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
Changes since v1:
- Set ctx.failed to report the mistake via exit code as well.

 t/unit-tests/test-lib.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

--
2.45.2

Comments

Phillip Wood July 23, 2024, 1:25 p.m. UTC | #1
On 23/07/2024 07:02, René Scharfe wrote:
> Checks outside of tests are caught at runtime and reported like this:
> 
>   Assertion failed: (ctx.running), function test_assert, file test-lib.c, line 267.
> 
> The assert() call aborts the unit test and doesn't reveal the location
> or even the type of the offending check, as test_assert() is called by
> all of them.
> 
> Handle it like the opposite case, a test without any checks: Don't
> abort, but report the location of the actual check, along with a message
> explaining the situation.  The output for example above becomes:
> 
>   # BUG: check outside of test at t/helper/test-example-tap.c:75
> 
> ... and the unit test program continues and indicates the error in its
> exit code at the end.

This is a nice idea and looks good

Thanks

Phillip

> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
> Changes since v1:
> - Set ctx.failed to report the mistake via exit code as well.
> 
>   t/unit-tests/test-lib.c | 7 ++++++-
>   1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
> index 3c513ce59a..989dc758e6 100644
> --- a/t/unit-tests/test-lib.c
> +++ b/t/unit-tests/test-lib.c
> @@ -264,7 +264,12 @@ static void test_todo(void)
> 
>   int test_assert(const char *location, const char *check, int ok)
>   {
> -	assert(ctx.running);
> +	if (!ctx.running) {
> +		test_msg("BUG: check outside of test at %s",
> +			 make_relative(location));
> +		ctx.failed = 1;
> +		return 0;
> +	}
> 
>   	if (ctx.result == RESULT_SKIP) {
>   		test_msg("skipping check '%s' at %s", check,
> --
> 2.45.2
>
diff mbox series

Patch

diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
index 3c513ce59a..989dc758e6 100644
--- a/t/unit-tests/test-lib.c
+++ b/t/unit-tests/test-lib.c
@@ -264,7 +264,12 @@  static void test_todo(void)

 int test_assert(const char *location, const char *check, int ok)
 {
-	assert(ctx.running);
+	if (!ctx.running) {
+		test_msg("BUG: check outside of test at %s",
+			 make_relative(location));
+		ctx.failed = 1;
+		return 0;
+	}

 	if (ctx.result == RESULT_SKIP) {
 		test_msg("skipping check '%s' at %s", check,