Added Unt Tests

This commit is contained in:
David Schuldenfrei 2018-07-15 01:46:48 +03:00
parent 2498ef7ef0
commit 2102d193d8
5 changed files with 224 additions and 0 deletions

View File

@ -283,6 +283,9 @@ if (gtest_build_tests)
cxx_executable(gtest_list_tests_unittest_ test gtest)
py_test(gtest_list_tests_unittest)
cxx_executable(gtest_list_output_unittest_ test gtest)
py_test(gtest_list_output_unittest)
cxx_executable(gtest_output_test_ test gtest)
py_test(gtest_output_test)

View File

@ -75,6 +75,7 @@ EXTRA_DIST += \
test/gtest_filter_unittest_.cc \
test/gtest_help_test_.cc \
test/gtest_list_tests_unittest_.cc \
test/gtest_list_output_unittest_.cc \
test/gtest_main_unittest.cc \
test/gtest_no_test_unittest.cc \
test/gtest_output_test_.cc \
@ -104,6 +105,7 @@ EXTRA_DIST += \
test/gtest_filter_unittest.py \
test/gtest_help_test.py \
test/gtest_list_tests_unittest.py \
test/gtest_list_output_unittest.py \
test/gtest_output_test.py \
test/gtest_output_test_golden_lin.txt \
test/gtest_shuffle_test.py \

View File

@ -266,6 +266,21 @@ py_test(
deps = [":gtest_test_utils"],
)
cc_binary(
name = "gtest_list_output_unittest_",
testonly = 1,
srcs = ["gtest_list_output_unittest_.cc"],
deps = ["//:gtest"],
)
py_test(
name = "gtest_list_output_unittest",
size = "small",
srcs = ["gtest_list_output_unittest.py"],
data = [":gtest_list_output_unittest_"],
deps = [":gtest_test_utils"],
)
cc_binary(
name = "gtest_shuffle_test_",
srcs = ["gtest_shuffle_test_.cc"],

View File

@ -0,0 +1,151 @@
#!/usr/bin/env python
#
# Copyright 2006, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Unit test for Google Test's --gtest_list_tests flag.
A user can ask Google Test to list all tests by specifying the
--gtest_list_tests flag. If output is requested, via --gtest_output=xml
or --gtest_output=json, the tests are listed, with extra information in the
output file.
This script tests such functionality by invoking gtest_list_output_unittest_
(a program written with Google Test) the command line flags.
"""
import os
import re
import gtest_test_utils
import gtest_xml_test_utils
GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
GTEST_OUTPUT_FLAG = '--gtest_output'
EXPECTED_XML = """<\?xml version="1.0" encoding="UTF-8"\?>
<testsuites tests="2" name="AllTests">
<testsuite name="FooTest" tests="2">
<testcase name="Test1" file=".*gtest_list_output_unittest_.cc" line="43" />
<testcase name="Test2" file=".*gtest_list_output_unittest_.cc" line="46" />
</testsuite>
</testsuites>
"""
EXPECTED_JSON = """{
"tests": 2,
"name": "AllTests",
"testsuites": \[
{
"name": "FooTest",
"tests": 2,
"testsuite": \[
{
"name": "Test1",
"file": ".*gtest_list_output_unittest_.cc",
"line": 43
},
{
"name": "Test2",
"file": ".*gtest_list_output_unittest_.cc",
"line": 46
}
\]
}
\]
}
"""
class GTestListTestsOutputUnitTest(gtest_test_utils.TestCase):
"""
Unit test for Google Test's list tests with output to file functionality.
"""
def testXml(self):
"""Verifies XML output for listing tests in a Google Test binary.
Runs a test program that generates an empty XML output, and
tests that the XML output is expected.
"""
self._TestOutput('xml', EXPECTED_XML)
def testJSON(self):
"""Verifies XML output for listing tests in a Google Test binary.
Runs a test program that generates an empty XML output, and
tests that the XML output is expected.
"""
self._TestOutput('json', EXPECTED_JSON)
def _GetOutput(self, format):
"""
Retrieves the content from the output file
"""
file_path = os.path.join(gtest_test_utils.GetTempDir(),
'test_out.' + format)
gtest_prog_path = gtest_test_utils.GetTestExecutablePath('gtest_list_output_unittest_')
command = ([gtest_prog_path,
'%s=%s:%s' % (GTEST_OUTPUT_FLAG, format, file_path),
'--gtest_list_tests'])
environ_copy = os.environ.copy()
p = gtest_test_utils.Subprocess(
command,
env=environ_copy,
working_dir=gtest_test_utils.GetTempDir())
self.assert_(p.exited)
self.assertEquals(0, p.exit_code)
with open(file_path) as f:
result = f.read()
return result
def _TestOutput(self, format, expected_output):
"""
Tests that the output is similar to the expected
"""
actual = self._GetOutput(format)
actual_lines = actual.splitlines()
expected_lines = expected_output.splitlines()
line_count = 0
for actual_line in actual_lines:
expected_line = expected_lines[line_count]
expected_line_re = re.compile(expected_line.strip())
self.assert_(
expected_line_re.match(actual_line.strip()),
('actual output of "%s",\n'
'which does not match expected regex of "%s"\n'
'on line %d' %
(actual, expected_output, line_count))
)
line_count = line_count + 1
if __name__ == '__main__':
os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
gtest_test_utils.Main()

View File

@ -0,0 +1,53 @@
// Copyright 2006, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: david.schuldenfrei@gmail.com (David Schuldenfrei)
// Unit test for Google Test's --gtest_list_tests and --gtest_output flag.
//
// A user can ask Google Test to list all tests that will run,
// and have the output saved in a Json/Xml file.
// The tests will not be run after listing.
//
// This program will be invoked from a Python unit test.
// Don't run it directly.
#include "gtest/gtest.h"
TEST(FooTest, Test1) {
}
TEST(FooTest, Test2) {
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}