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

Keep console open after the tests #477

Closed
nicolaje opened this issue Aug 7, 2015 · 8 comments
Closed

Keep console open after the tests #477

nicolaje opened this issue Aug 7, 2015 · 8 comments

Comments

@nicolaje
Copy link

nicolaje commented Aug 7, 2015

With Visual Sutdio, running Catch tests will close the console after execution, and you can't see the results of the tests unless you set some unintuitive option in a dark corner of the project settings and use a new keyboard shortcut to run the project.

It is more a Visual Studio issue, but it would be great if by default Catch would keep the console open with a dumb getchar(); or at least adding a command line option to keep the console open.

@DavidEGrayson
Copy link
Contributor

I was going to make a pull request for this feature at some point, because it would also be useful for catching memory leaks on Mac OS X. That page explains:

Most unit testing code executes the desired code paths and exits. Although this is perfectly normal for unit testing, it creates a problem for the leaks tool, which needs time to analyze the process memory space. To fix this problem, you should make sure your unit-testing code does not exit immediately upon completing its tests. You can do this by putting the process to sleep indefinitely instead of exiting normally.

Until then, @nicolaje, you can define your own main function (looking at the one in catch.hpp as a reference) and do whatever you want at the end of the tests.

@qwoodmansee
Copy link

qwoodmansee commented May 5, 2016

@nicolaje I just ran into this as well - for anyone else who runs into this, David provided me the insight I needed to solve it. Steps Listed below:

  1. Change CATCH_CONFIG_MAIN to CATCH_CONFIG_RUNNER
  2. Add this main to that file:
int main(int argc, char* const argv[]) {
    int result = Catch::Session().run(argc, argv);

    while (true);
    return result;
}

Note that you'll have to kill the program via visual studio unless you implement another way to end the while loop.

@dvirtz
Copy link
Contributor

dvirtz commented May 5, 2016

Alternatively, add switch /SUBSYSTEM:CONSOLE to the linker
(Properties/Linker/System/SubSystem).
When running without debugging, this will print "Press any key to continue
. . ." and wait.

On 5 May 2016 at 20:11, qwoodmansee notifications@github.com wrote:

@nicolaje https://github.com/nicolaje I just ran into this as well -
for anyone else who runs into this, David provided me the insight I needed
to solve it. Steps Listed below:

  1. Change CATCH_CONFIG_MAIN to CATCH_CONFIG_RUNNER
    2.

    Add this main to that file:
    `int main(int argc, char* const argv[]) {
    int result = Catch::Session().run(argc, argv);

    while (true);
    return result;
    }`

Note that you'll have to kill the program via visual studio unless you
implement another way to end the while loop.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#477 (comment)

@DavidEGrayson
Copy link
Contributor

DavidEGrayson commented May 5, 2016

This is what I did:

#define CATCH_CONFIG_RUNNER
#include <test_helper.h>

int main(int argc, char ** argv)
{
    // If the last argument is "-p", then pause after the tests are run.
    // This allows us to run "leaks" on Mac OS X to check for memory leaks.
    bool pause_after_test = false;
    if (argc && std::string(argv[argc - 1]) == "-p")
    {
        pause_after_test = true;
        argc--;
    }

    int result = Catch::Session().run(argc, argv);

    if (pause_after_test)
    {
        printf("Press enter to continue.");
        std::string s;
        std::cin >> s;
    }

    return result;
}

Waiting for user input is nicer than just spinning in an infinite loop, taking up CPU time.

@mjammer
Copy link

mjammer commented May 10, 2016

Alternatively,with Visual Sutdio,you can also replace while(true); with system("pause");.

@nicolaje I just ran into this as well - for anyone else who runs into this, David provided me the insight I needed to solve it. Steps Listed below:
Change CATCH_CONFIG_MAIN to CATCH_CONFIG_RUNNER
Add this main to that file:
int main(int argc, char* const argv[]) {
int result = Catch::Session().run(argc, argv);
while (true);
return result;
}

@josh-ent
Copy link

It wouldn't be too much hassle, I assume, to add a --waitkey switch and modify the auto-generated main routines a little?

As it stands, I've beggered with my own copy a little and added a wait directly into catch.hpp (in blatant violation of the polite request not to change the merged file, sorry).

@40tude
Copy link

40tude commented Feb 15, 2017

Bonjour
I'm just getting starting with unit testing and CATCH
I'm working mostly under Windows with msvc 2017RC & msvc 2015-3
Re-using @DavidEGrayson proposition I'm able to keep the console on screen during debug session (many thanks for the tips).
However I lost the colors.
Do you confirm this is what is expected ?
Is there anything we could do to get the colors back in the console of the debug session?
What is so special with that console which make we loose the colors? I do not have a clear understanding on what is going on here. Any explaination are welcome here :-)

FYI : everything works fine and I get the colors when I run the application out of the debug session (either from the IDE or directly invoking the test code from a console).

Any advise/idea are more than welcome
Best regards, Philippe

PS : like some others I support the idea of a switch that would let the console on screen during debug sessions

@philsquared
Copy link
Collaborator

Sorry this request has been around for quite a while. I know it's a simple feature to add, and I had intended to do so.

I've now added it as the --wait-for-keypress exit option (you can also pass it start to wait on start-up, or both to wait on startup and exit).
It just prints a message and calls std::getchar() - so effectively waits for enter/ return.

See https://github.com/philsquared/Catch/blob/master/docs/command-line.md#wait-for-keypress

@philsquared philsquared added the Resolved - pending review Issue waiting for feedback from the original author label Aug 15, 2017
@horenmar horenmar closed this as completed Nov 3, 2017
@horenmar horenmar removed the Resolved - pending review Issue waiting for feedback from the original author label Nov 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants