-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Typed Tests #357
Comments
This comes up from time to time. Thanks for bringing it up again - it helps to get an idea of how many would find it useful. |
+1 |
+1 |
+1 |
@fsolenberg @wichtounet Are you able to report whether the @philsquared hasn't commented on whether he'd accept that code as a patch yet, but more votes (either way!) might help... Best regards, |
@garethsb This works like a charm :) I add to change CATCH_TEST_CASE to TEST_CASE and CATCH_SECTION to SECTION though, this may come from different version of Catch. Thanks a lot. I will definitely use it in my template math library. |
@wichtounet That's great! By the way, Catch allows you to choose whether it provides short names ( I guess I should work up my sketch into a proper PR that supports both forms... |
One thing that bugs me with this proposal is that it doesn't allow different ways of constructing the object to be tested - they would all have to be constructed the same way. In that sense, I think it would become a framework type solution, which I would have to work around for some scenarios. I like how straightforward it is to use SECTIONs in Catch. Another way to approach typed tests would be to do something similar for the setup/teardown as well? Such as:
So the actual set of tests to run would be the cartesian product of INIT_SECTIONs and SECTIONs (there's likely a DEINIT_SECTION as well, and sure, the naming can be discussed). I haven't looked at the Catch source so I have no idea whether this is feasible or not. Then TEMPLATE_TEST_CASE_2, referred to above, would become:
Yes, a little bit more verbose, but nothing that can't be folded into another macro. :) e.g.:
That's one idea. |
@fsolenberg Could you check your formatting ? All the angle brackets have gone away. |
@wichtounet Thanks for pointing that out. |
This is good input and I think I understand the motivation of @fsolenberg to argue for However I believe that these template test cases should work like C++ templates; the types under test must meet all the requirements of the concept being tested. If the types have a few different behaviours, those parts can be tested independently by other test cases specific to that type or subset of types. In the template <typename T> T foo_construct();
template <>
reference_impl foo_construct<reference_impl>() { return reference_impl("bin/coeffs_ref"); }
template <>
optimized_impl foo_construct<optimized_impl>() { return optimized_impl(); }
TEMPLATE_TEST_CASE_2("foo", "", Impl, reference_impl, optimized_impl )
{
Impl foo = foo_construct<Impl>();
SECTION("bar") {
REQUIRE(foo.size() == 32);
REQUIRE(foo.bar({1, 2, 3, 4}) == 76);
... (I'm not actually sure in @fsolenberg pseudo-code how the Best regards, |
Yes, you're right, looking through the code now I can see my suggested approach would require quite heavy-handed restructuring, and when I think about it, wonder if it is at all possible? While the template specialization would get the job done, this is exactly the type of work around I was referring to - the framework dictates how the code must be written, it's not me in charge anymore, and readability suffers. Ho-humm... |
Your example use case is testing constructed instances of types, and doesn't use the type-name itself at all. To be honest I'd write these kind of tests using existing Catch infrastructure: template <typename T > void foo_test(T foo);
TEST_CASE("foo")
{
foo_test(reference_impl("bin/coeffs_ref"));
foo_test(optimized_impl());
}
template <typename T > void foo_test(T foo) {
SECTION("bar") {
REQUIRE(foo.size() == 32);
REQUIRE(foo.bar({1, 2, 3, 4}) == 76);
... Actually we can wrap that up in a macro to allow you to write: TEMPLATE_FUNCTION_TEST_CASE_2("foo", foo, reference_impl("bin/coeffs_ref"), optimized_impl())
{
SECTION("bar") {
REQUIRE(foo.size() == 32);
REQUIRE(foo.bar({1, 2, 3, 4}) == 76);
... However, the template test cases that I need are more general - I need to be able to test other properties of the type (does it have a certain nested type, can I construct it from two ints, etc.). Best regards, |
@garethsb Finally, there seems to be something wrong in your implementation. Now that I have many templated test cases, some of them are not always launched :s I have added some prints into some of them and I don't see the prints. I've also add REQUIRE(false) into some tests, but that does not show anything on the console rather than all tests passed... I didn't have these kinds of issue before using template_test_case_2. Looking at the proprocessed code, I cannot find anything wrong, but I'm nowhere near a Catch pro. I have about 500 test cases and almost all of them are templated, could I be hitting some limits ? |
I suspect it's a problem with using We need to get it into a file scope to solve this, e.g. using anonymous namespace. |
I also came to the same conclusion :) Simply adding static to the template function fixed the issue :) |
@philsquared is there any feature ticket or something like that for value parameterization so one can keep track of the work on that? |
Sorry. This dropped off my radar until #565 referenced it recently. |
I don't get the issue here. We only need each section to be executed once since there is a section for each template type and each section simply calls the templated test function with the correct type. No ? |
It's one of the areas that simplicity of the |
@philsquared Sorry, I think we are not talking about the same thing :s I was referring to the TEMPLATE_TEST_CASE_n and you were referring to the simpler example for March 17, sorry. |
Ah, the perils of (me) dropping in on the thread so late - sorry! I did try and qualify it with "March 17th proposal" :-) |
👍 |
This is in as of 450dd05 |
When performing the same tests over different types, it would be useful to have a feature similar to Google's Typed Tests: https://code.google.com/p/googletest/wiki/AdvancedGuide#Typed_Tests
The text was updated successfully, but these errors were encountered: