

Catch: A modern, C++-native, header-only, framework for unit tests - tbrock
https://github.com/philsquared/Catch

======
Svenstaro
To be honest, I prefer bandit[1], it uses lambdas instead of macros which I
consider more modern and it has a nice syntax to it. Anyone interested in
catch should also check out bandit.

[1] [http://banditcpp.org/index.html](http://banditcpp.org/index.html)

~~~
tveita
One advantage of Catch is that you can use native C++ expressions in your
assertions, so instead of

    
    
      AssertThat(guitar->sound(), Equals(sounds::clean));
    

you can write

    
    
      CHECK(guitar->sound() == sounds::clean);
    

And you'll still get sensible output when it fails, showing the expression and
the result of guitar->sound().

~~~
banachtarski
This isn't so much an advantage as a preference.

See
[https://github.com/joakimkarlsson/snowhouse#assertions](https://github.com/joakimkarlsson/snowhouse#assertions)

------
chadaustin
I don't really see the advantage of Catch over [http://unittest-
cpp.sourceforge.net/](http://unittest-cpp.sourceforge.net/)

UnitTest++ was written by Noel Llopis, is small, tasteful, fast, and supports
fixtures (and nested fixtures) via structs and struct inheritance.

Honest question: Why would I use Catch instead?

~~~
stinos
I'm in the same boat (having used UnitTest++ for years) and I wonder the same,
though I do see some points already:

\- adding a custom main() to unittest++ was pretty messy

\- unittest++ is not single-header, meaning you have to recompile all flavours
everytime you switch toolsets

\- that nested sections stuff looks good and readable, so even though it might
be doable with unittest++, this really attracts me more

That being said, I would like to hear from the author or anyone else who can
compare them. But likely I'm going to use this for the next project just to
check it out.

------
rdw
This looks hella good, I particularly am intrigued by the nested
setup/teardown system. The natural assert syntax is interesting, too, amazing
what macros can accomplish.

Appropriately, the first question that came to mind was, "what's the catch?"
:)

~~~
tbrock
Yup. I found it interesting after a co-worker pointed it out to me because it
looks and works kind of like RSpec. We are currently considering using it for
a new project and it seems mature enough to give it a go.

I'm currently toying with writing a JSONReporter so we can use that as an
additional test result output format and I agree, so far it seems to be a
really well done project.

~~~
wichert
FWIW I've been using Catch for a few years now, and I find it mature enough
for real world usage.

~~~
tbrock
Good to know, thanks!

------
pvidler
It looks like a nice test framework, but it doesn't appear to help at all with
the really hard problems you face with testing C++ -- isolating the unit under
test for separate compilation, and the development and inclusion of
mocks/stubs.

Anyone know of a really good (preferably simple and pragmatic) C++
mocking/stubbing framework to go with this?

~~~
Ono-Sendai
Why do you want to compile the unit test separately?

~~~
pvidler
Because that's what makes it a unit test -- you only want the unit, the tests
and any stubs/mocks in the executable. Anything more and you have an
integration test on your hands.

Pulling a single unit out of a legacy codebase can be difficult, especially
when you don't want to actually move, copy, or modify the source-files;
stubbing out the dependencies and writing mock objects to verify your
expectations of what the code should be doing are also very time-consuming.

Those are the hard parts of unit testing C++, and I wonder if anyone has found
a good way of doing it (with a framework, or just a technique).

------
npsimons
Not to steal your thunder, but TUT (Templated Unit Tests) does this as well,
and I've found it works very nicely even in embedded and cross-platform:

[https://mrzechonek.github.io/tut-
framework/](https://mrzechonek.github.io/tut-framework/)

It's lightweight, easy to setup for separated tests or built in tests, and
simple to use. Doesn't add anything to help with mocking, but you can do that
yourself.

EDIT: To followup, I created a C++ template that leverages TUT and binfmtc
that I could use to rapidly iterate with. I used it for working out solutions
to problems in "Thinking in C++"; it's posted at
[http://hardcorehackers.com/~npsimons/Template.hh](http://hardcorehackers.com/~npsimons/Template.hh)

------
borland
I _really_ like the look of the "Sections" for managing setup/teardown of
tests. I'm envious and next time I'm hacking on our in-house C# test framework
I'm going to try very hard to implement them! Neat!

------
yoanizer
Great work. I love the fact that this encourages writing tests that are
readable/documenting.

Definitely gonna give this a try.

Any recommended test runner to go with it?

~~~
awestroke
There's a test runner included as a macro that defines main

------
larssonvomdach
A coworker of mine is working on something similar: [https://github.com/Kosta-
Github/cute](https://github.com/Kosta-Github/cute)

------
c_ehlen
This looks really good. Do you plan to include BeforeEach and AfterEach? I'll
try this out and maybe move my projects to this.

I started working on something similar[1] a few days ago just as a proof of
concept.

[1]
[https://github.com/cEhlen/CUT/blob/master/example.cpp](https://github.com/cEhlen/CUT/blob/master/example.cpp)

------
rikkus
A similar thing I knocked up a few years ago. Lacking in features and, well,
most things - except it does have a pretty Qt based runner:
[http://www.rikkus.info/cplusplus-unit-testing-
framework](http://www.rikkus.info/cplusplus-unit-testing-framework)

------
talles
Sorry for the off-topicness but I have to say...

I _loved_ the project logo

------
voltagex_
I'm not a C++ programmer but this looks really neat and tidy. How does it
work?

~~~
astrodust
Any sufficiently advanced C++ template code is indistinguishable magic.

------
X-Istence
This looks very intriguing. Right now I am using Google Test for my projects,
but a single header file for creating tests is awesome :-)

