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

[question] Is there a way to always have 0 as the exit code regardless of test results? #59

Closed
theldoria opened this issue Mar 23, 2017 · 2 comments

Comments

@theldoria
Copy link

I need an option to define exit code value. I am personally interest in setting it to zero, independent from test results, but would be happy to be able to set it for success and failure separately.

Background:
I am currently using this framework within emacs org-mode in an source code block, which can be executed within emacs, adding the result into the document. This, however, does not work if the exit code is not zero, because noting is added. But exactly that case would be of interest.

Example of what I need to do:

#+HEADER: :exports both :results output
#+HEADER: :includes <iostream> <unistd.h>
#+HEADER: :main no
#+HEADER: :flags -I. -std=c++03 -pedantic -Werror
#+BEGIN_SRC C++
#define DOCTEST_CONFIG_IMPLEMENT//_WITH_MAIN
#include "doctest.h"

static int factorial(int number)
{
   return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("testing the factorial function")
{
   CHECK(factorial(0) == 1);
   CHECK(factorial(1) == 1);
   CHECK(factorial(2) == 2);
   CHECK(factorial(3) == 6);
   CHECK(factorial(10) == 3628800);
}

int main(int argc, char** argv) {
    doctest::Context context; // initialize

    // defaults
    context.addFilter("test-case-exclude", "*math*"); // exclude test cases with "math" in their name
    context.setOption("abort-after", 5);              // stop test execution after 5 failed assertions
    context.setOption("sort", "name");                // sort the test cases by their name

    context.applyCommandLine(argc, argv);

    // overrides
    context.setOption("no-breaks", true);             // don't break in the debugger when assertions fail

    int res = context.run(); // run

    if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this
        return res;          // propagate the result of the tests
    
    int client_stuff_return_code = 0;
    // your program - if the testing framework is integrated in your production code
    
    return 0;//res + client_stuff_return_code; // the result from doctest is propagated here as well
}
#+END_SRC

#+RESULTS:
#+begin_example
[doctest] doctest version is "1.1.4"
[doctest] run with "--help" for options
===============================================================================
/tmp/babel-6771qy1/C-src-6771uQC.cpp(16)
testing the factorial function

/tmp/babel-6771qy1/C-src-6771uQC.cpp(18) FAILED! 
  CHECK( factorial(0) == 1 )
with expansion:
  CHECK( 0 == 1 )

===============================================================================
[doctest] test cases:    1 |    0 passed |    1 failed |    0 skipped
[doctest] assertions:    5 |    4 passed |    1 failed |
#+end_example
@onqtam
Copy link
Member

onqtam commented Mar 24, 2017

There is the --no-exitcode command line option which does exactly that - always returns 0.

You could set it as a default (before the command line parsing) or as an override (after the command line parsing) with context.setOption("no-exitcode", true);

Does this help?

Also note that the calls to setOption() I've put in the examples of a user-provided main() are not really needed.

@theldoria
Copy link
Author

Hey, thanks. Yes that indeed did the trick.

In case someone else also cares, I now have this:

#+HEADER: :exports both :results output
#+HEADER: :includes <iostream> <unistd.h>
#+HEADER: :main no
#+HEADER: :flags -I. -std=c++03 -pedantic -Werror
#+HEADER: :cmdline --no-exitcode
#+BEGIN_SRC C++
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#define DOCTEST_CONFIG_COLORS_ANSI
#include "doctest.h"

static int factorial(int number)
{
   return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("testing the factorial function")
{
   CHECK(factorial(0) == 1);
   CHECK(factorial(1) == 1);
   CHECK(factorial(2) == 2);
   CHECK(factorial(3) == 6);
   CHECK(factorial(10) == 3628800);
}
#+END_SRC

#+RESULTS:
#+begin_example
[doctest] doctest version is "1.1.4"
[doctest] run with "--help" for options
===============================================================================
c:/Windows/TEMP/babel-12648gy4/C-src-126486SN.cpp(17)
testing the factorial function

c:/Windows/TEMP/babel-12648gy4/C-src-126486SN.cpp(19) FAILED! 
  CHECK( factorial(0) == 1 )
with expansion:
  CHECK( 0 == 1 )

===============================================================================
[doctest] test cases:    1 |    0 passed |    1 failed |    0 skipped
[doctest] assertions:    5 |    4 passed |    1 failed |
#+end_example

@onqtam onqtam changed the title Provide exit code option [question] Is there a way to always have 0 as the exit code regardless of test results? Mar 24, 2017
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

2 participants