Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking result of EXPECT_* macros in GTest or run code when ASSERT_* failed
    primarykey
    data
    text
    <p>I have written C++ tests with GTest which basically work like this</p> <pre><code>MyData data1 = runTest(inputData); MyData data2 = loadRegressionData(); compareMyData(data1,data2); </code></pre> <p>with</p> <pre><code>void compareMyData(MyData const&amp; data1, MyData const&amp; data2) { ASSERT_EQ(data1.count, data2.count); //pseudo: foreach element in data1/data2: EXPECT_EQ(data1.items[i], data2.items[i]); } </code></pre> <p>Now I would like to save the <code>data1</code> contents to a file IFF the test fails and I don't see an elegant solution yet.</p> <hr> <p>First approach: <strong>Make <code>compareMyData</code> return the comparison result.</strong> This can't work with the <code>ASSERT_EQ</code> which is fatal. Writing <code>if (!EXPECT_EQ(...))</code> doesn't compile so the only way I found is </p> <pre><code>bool compareMyData(MyData const&amp; data1, MyData const&amp; data2) { EXPECT_EQ(data1.count, data2.count); if (data1.count != data2.count) return false; //pseudo: foreach element in data1/data2: { EXPECT_EQ(data1.items[i], data2.items[i]); if (data1.items[i]!= data2.items[i]) return false; } } </code></pre> <p>Not very elegant :-(</p> <hr> <p>Second idea: <strong>Run code when the test failed</strong></p> <p>I know I can implement <code>::testing::EmptyTestEventListener</code> and get notified if a test fails, but that doesn't give me the data I want to write to file and it is "far away" from the place I'd like it to have. So my question here is: Is there a way to run code at the end of a test if it failed (e.g. catching an exception?).</p> <hr> <p>To ask more general: how would you solve this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload