sample9_unittest.cc (googletest-release-1.11.0) | : | sample9_unittest.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 31 | skipping to change at line 31 | |||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// This sample shows how to use Google Test listener API to implement | // This sample shows how to use Google Test listener API to implement | |||
// an alternative console output and how to use the UnitTest reflection API | // an alternative console output and how to use the UnitTest reflection API | |||
// to enumerate test cases and tests and to inspect their results. | // to enumerate test suites and tests and to inspect their results. | |||
#include <stdio.h> | #include <stdio.h> | |||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | |||
using ::testing::EmptyTestEventListener; | using ::testing::EmptyTestEventListener; | |||
using ::testing::InitGoogleTest; | using ::testing::InitGoogleTest; | |||
using ::testing::Test; | using ::testing::Test; | |||
using ::testing::TestCase; | ||||
using ::testing::TestEventListeners; | using ::testing::TestEventListeners; | |||
using ::testing::TestInfo; | using ::testing::TestInfo; | |||
using ::testing::TestPartResult; | using ::testing::TestPartResult; | |||
using ::testing::TestSuite; | ||||
using ::testing::UnitTest; | using ::testing::UnitTest; | |||
namespace { | namespace { | |||
// Provides alternative output mode which produces minimal amount of | // Provides alternative output mode which produces minimal amount of | |||
// information about tests. | // information about tests. | |||
class TersePrinter : public EmptyTestEventListener { | class TersePrinter : public EmptyTestEventListener { | |||
private: | private: | |||
// Called before any test activity starts. | // Called before any test activity starts. | |||
void OnTestProgramStart(const UnitTest& /* unit_test */) override {} | void OnTestProgramStart(const UnitTest& /* unit_test */) override {} | |||
// Called after all test activities have ended. | // Called after all test activities have ended. | |||
void OnTestProgramEnd(const UnitTest& unit_test) override { | void OnTestProgramEnd(const UnitTest& unit_test) override { | |||
fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED"); | fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED"); | |||
fflush(stdout); | fflush(stdout); | |||
} | } | |||
// Called before a test starts. | // Called before a test starts. | |||
void OnTestStart(const TestInfo& test_info) override { | void OnTestStart(const TestInfo& test_info) override { | |||
fprintf(stdout, | fprintf(stdout, "*** Test %s.%s starting.\n", test_info.test_suite_name(), | |||
"*** Test %s.%s starting.\n", | ||||
test_info.test_case_name(), | ||||
test_info.name()); | test_info.name()); | |||
fflush(stdout); | fflush(stdout); | |||
} | } | |||
// Called after a failed assertion or a SUCCEED() invocation. | // Called after a failed assertion or a SUCCEED() invocation. | |||
void OnTestPartResult(const TestPartResult& test_part_result) override { | void OnTestPartResult(const TestPartResult& test_part_result) override { | |||
fprintf(stdout, | fprintf(stdout, "%s in %s:%d\n%s\n", | |||
"%s in %s:%d\n%s\n", | ||||
test_part_result.failed() ? "*** Failure" : "Success", | test_part_result.failed() ? "*** Failure" : "Success", | |||
test_part_result.file_name(), | test_part_result.file_name(), test_part_result.line_number(), | |||
test_part_result.line_number(), | ||||
test_part_result.summary()); | test_part_result.summary()); | |||
fflush(stdout); | fflush(stdout); | |||
} | } | |||
// Called after a test ends. | // Called after a test ends. | |||
void OnTestEnd(const TestInfo& test_info) override { | void OnTestEnd(const TestInfo& test_info) override { | |||
fprintf(stdout, | fprintf(stdout, "*** Test %s.%s ending.\n", test_info.test_suite_name(), | |||
"*** Test %s.%s ending.\n", | ||||
test_info.test_case_name(), | ||||
test_info.name()); | test_info.name()); | |||
fflush(stdout); | fflush(stdout); | |||
} | } | |||
}; // class TersePrinter | }; // class TersePrinter | |||
TEST(CustomOutputTest, PrintsMessage) { | TEST(CustomOutputTest, PrintsMessage) { | |||
printf("Printing something from the test body...\n"); | printf("Printing something from the test body...\n"); | |||
} | } | |||
TEST(CustomOutputTest, Succeeds) { | TEST(CustomOutputTest, Succeeds) { | |||
SUCCEED() << "SUCCEED() has been invoked from here"; | SUCCEED() << "SUCCEED() has been invoked from here"; | |||
} | } | |||
TEST(CustomOutputTest, Fails) { | TEST(CustomOutputTest, Fails) { | |||
EXPECT_EQ(1, 2) | EXPECT_EQ(1, 2) | |||
<< "This test fails in order to demonstrate alternative failure messages"; | << "This test fails in order to demonstrate alternative failure messages"; | |||
} | } | |||
} // namespace | } // namespace | |||
int main(int argc, char **argv) { | int main(int argc, char** argv) { | |||
InitGoogleTest(&argc, argv); | InitGoogleTest(&argc, argv); | |||
bool terse_output = false; | bool terse_output = false; | |||
if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 ) | if (argc > 1 && strcmp(argv[1], "--terse_output") == 0) | |||
terse_output = true; | terse_output = true; | |||
else | else | |||
printf("%s\n", "Run this program with --terse_output to change the way " | printf("%s\n", | |||
"Run this program with --terse_output to change the way " | ||||
"it prints its output."); | "it prints its output."); | |||
UnitTest& unit_test = *UnitTest::GetInstance(); | UnitTest& unit_test = *UnitTest::GetInstance(); | |||
// If we are given the --terse_output command line flag, suppresses the | // If we are given the --terse_output command line flag, suppresses the | |||
// standard output and attaches own result printer. | // standard output and attaches own result printer. | |||
if (terse_output) { | if (terse_output) { | |||
TestEventListeners& listeners = unit_test.listeners(); | TestEventListeners& listeners = unit_test.listeners(); | |||
// Removes the default console output listener from the list so it will | // Removes the default console output listener from the list so it will | |||
skipping to change at line 151 | skipping to change at line 146 | |||
// Counts failed tests that were not meant to fail (those without | // Counts failed tests that were not meant to fail (those without | |||
// 'Fails' in the name). | // 'Fails' in the name). | |||
if (test_info.result()->Failed() && | if (test_info.result()->Failed() && | |||
strcmp(test_info.name(), "Fails") != 0) { | strcmp(test_info.name(), "Fails") != 0) { | |||
unexpectedly_failed_tests++; | unexpectedly_failed_tests++; | |||
} | } | |||
} | } | |||
} | } | |||
// Test that were meant to fail should not affect the test program outcome. | // Test that were meant to fail should not affect the test program outcome. | |||
if (unexpectedly_failed_tests == 0) | if (unexpectedly_failed_tests == 0) ret_val = 0; | |||
ret_val = 0; | ||||
return ret_val; | return ret_val; | |||
} | } | |||
End of changes. 11 change blocks. | ||||
17 lines changed or deleted | 11 lines changed or added |