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

How to test for exit(EXIT_CODE)? #538

Open
yuw444 opened this issue May 7, 2024 · 2 comments
Open

How to test for exit(EXIT_CODE)? #538

yuw444 opened this issue May 7, 2024 · 2 comments

Comments

@yuw444
Copy link

yuw444 commented May 7, 2024

Hi, I like to test for the exit code.

Below is a minimum example

#include <criterion/criterion.h>
#include <stdio.h>

void foo(int a)
{
  if(a == 1) exit(1);
  if(a ==2) exit(2);
  return;
}

how to capture the exit code, such as 1, 2? I have try cr_fail(), cr_exit() without luck.

Thanks

@Marek324
Copy link

Think I'm gonna pass on using criterion after finding out this wasn't added half a year later. Need it right now.

@rgomes1
Copy link

rgomes1 commented Oct 28, 2024

Fellow criterion user here, have you looked at the .exit_code directive?
Here is an example from the criterion source code:

Test(exit, expected_exit, .exit_code = 42)
{
exit(42);
}

The .exit_code directive allows a non zero exit status to be declared as the expected result for a given Unit Test. By default, zero (0) is the normal exit status when executing a Unit Test and a non zero status would fail the test.

where:
.exit_code Is the name of the directive.
= A numeric value indicating the expected exit status for the Unit Test. From the example, the Unit Test is behaving as expected when returning a value of 42 – therefore, the test would not be marked as a failure despite it returning a non zero exit code, since it matches what was declared in the directive. Specifying a zero value for this directive is allowed but not needed, since it is the implied default.

They also discuss this in their documentation, link is:
https://readthedocs.org/projects/criterion/downloads/pdf/latest/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants