Commit f5fa71f7 by vladlosev

Implements support for calling Test::RecordProperty() outside of a test.

parent 5f18b68b
...@@ -158,6 +158,7 @@ class StreamingListenerTest; ...@@ -158,6 +158,7 @@ class StreamingListenerTest;
class TestResultAccessor; class TestResultAccessor;
class TestEventListenersAccessor; class TestEventListenersAccessor;
class TestEventRepeater; class TestEventRepeater;
class UnitTestRecordPropertyTestHelper;
class WindowsDeathTest; class WindowsDeathTest;
class UnitTestImpl* GetUnitTestImpl(); class UnitTestImpl* GetUnitTestImpl();
void ReportFailureInUnknownLocation(TestPartResult::Type result_type, void ReportFailureInUnknownLocation(TestPartResult::Type result_type,
...@@ -381,20 +382,21 @@ class GTEST_API_ Test { ...@@ -381,20 +382,21 @@ class GTEST_API_ Test {
// non-fatal) failure. // non-fatal) failure.
static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
// Logs a property for the current test. Only the last value for a given // Logs a property for the current test, test case, or for the entire
// key is remembered. // invocation of the test program when used outside of the context of a
// These are public static so they can be called from utility functions // test case. Only the last value for a given key is remembered. These
// that are not members of the test fixture. // are public static so they can be called from utility functions that are
// The arguments are const char* instead strings, as Google Test is used // not members of the test fixture. Calls to RecordProperty made during
// on platforms where string doesn't compile. // lifespan of the test (from the moment its constructor starts to the
// // moment its destructor finishes) will be output in XML as attributes of
// Note that a driving consideration for these RecordProperty methods // the <testcase> element. Properties recorded from fixture's
// was to produce xml output suited to the Greenspan charting utility, // SetUpTestCase or TearDownTestCase are logged as attributes of the
// which at present will only chart values that fit in a 32-bit int. It // corresponding <testsuite> element. Calls to RecordProperty made in the
// is the user's responsibility to restrict their values to 32-bit ints // global context (before or after invocation of RUN_ALL_TESTS and from
// if they intend them to be used with Greenspan. // SetUp/TearDown method of Environment objects registered with Google
static void RecordProperty(const char* key, const char* value); // Test) will be output as attributes of the <testsuites> element.
static void RecordProperty(const char* key, int value); static void RecordProperty(const std::string& key, const std::string& value);
static void RecordProperty(const std::string& key, int value);
protected: protected:
// Creates a Test object. // Creates a Test object.
...@@ -463,7 +465,7 @@ class TestProperty { ...@@ -463,7 +465,7 @@ class TestProperty {
// C'tor. TestProperty does NOT have a default constructor. // C'tor. TestProperty does NOT have a default constructor.
// Always use this constructor (with parameters) to create a // Always use this constructor (with parameters) to create a
// TestProperty object. // TestProperty object.
TestProperty(const char* a_key, const char* a_value) : TestProperty(const std::string& a_key, const std::string& a_value) :
key_(a_key), value_(a_value) { key_(a_key), value_(a_value) {
} }
...@@ -478,7 +480,7 @@ class TestProperty { ...@@ -478,7 +480,7 @@ class TestProperty {
} }
// Sets a new value, overriding the one supplied in the constructor. // Sets a new value, overriding the one supplied in the constructor.
void SetValue(const char* new_value) { void SetValue(const std::string& new_value) {
value_ = new_value; value_ = new_value;
} }
...@@ -537,6 +539,7 @@ class GTEST_API_ TestResult { ...@@ -537,6 +539,7 @@ class GTEST_API_ TestResult {
private: private:
friend class TestInfo; friend class TestInfo;
friend class TestCase;
friend class UnitTest; friend class UnitTest;
friend class internal::DefaultGlobalTestPartResultReporter; friend class internal::DefaultGlobalTestPartResultReporter;
friend class internal::ExecDeathTest; friend class internal::ExecDeathTest;
...@@ -561,13 +564,16 @@ class GTEST_API_ TestResult { ...@@ -561,13 +564,16 @@ class GTEST_API_ TestResult {
// a non-fatal failure if invalid (e.g., if it conflicts with reserved // a non-fatal failure if invalid (e.g., if it conflicts with reserved
// key names). If a property is already recorded for the same key, the // key names). If a property is already recorded for the same key, the
// value will be updated, rather than storing multiple values for the same // value will be updated, rather than storing multiple values for the same
// key. // key. xml_element specifies the element for which the property is being
void RecordProperty(const TestProperty& test_property); // recorded and is used for validation.
void RecordProperty(const std::string& xml_element,
const TestProperty& test_property);
// Adds a failure if the key is a reserved attribute of Google Test // Adds a failure if the key is a reserved attribute of Google Test
// testcase tags. Returns true if the property is valid. // testcase tags. Returns true if the property is valid.
// TODO(russr): Validate attribute names are legal and human readable. // TODO(russr): Validate attribute names are legal and human readable.
static bool ValidateTestProperty(const TestProperty& test_property); static bool ValidateTestProperty(const std::string& xml_element,
const TestProperty& test_property);
// Adds a test part result to the list. // Adds a test part result to the list.
void AddTestPartResult(const TestPartResult& test_part_result); void AddTestPartResult(const TestPartResult& test_part_result);
...@@ -792,6 +798,10 @@ class GTEST_API_ TestCase { ...@@ -792,6 +798,10 @@ class GTEST_API_ TestCase {
// total_test_count() - 1. If i is not in that range, returns NULL. // total_test_count() - 1. If i is not in that range, returns NULL.
const TestInfo* GetTestInfo(int i) const; const TestInfo* GetTestInfo(int i) const;
// Returns the TestResult that holds test properties recorded during
// execution of SetUpTestCase and TearDownTestCase.
const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; }
private: private:
friend class Test; friend class Test;
friend class internal::UnitTestImpl; friend class internal::UnitTestImpl;
...@@ -880,6 +890,9 @@ class GTEST_API_ TestCase { ...@@ -880,6 +890,9 @@ class GTEST_API_ TestCase {
bool should_run_; bool should_run_;
// Elapsed time, in milliseconds. // Elapsed time, in milliseconds.
TimeInMillis elapsed_time_; TimeInMillis elapsed_time_;
// Holds test properties recorded during execution of SetUpTestCase and
// TearDownTestCase.
TestResult ad_hoc_test_result_;
// We disallow copying TestCases. // We disallow copying TestCases.
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);
...@@ -1165,6 +1178,10 @@ class GTEST_API_ UnitTest { ...@@ -1165,6 +1178,10 @@ class GTEST_API_ UnitTest {
// total_test_case_count() - 1. If i is not in that range, returns NULL. // total_test_case_count() - 1. If i is not in that range, returns NULL.
const TestCase* GetTestCase(int i) const; const TestCase* GetTestCase(int i) const;
// Returns the TestResult containing information on test failures and
// properties logged outside of individual test cases.
const TestResult& ad_hoc_test_result() const;
// Returns the list of event listeners that can be used to track events // Returns the list of event listeners that can be used to track events
// inside Google Test. // inside Google Test.
TestEventListeners& listeners(); TestEventListeners& listeners();
...@@ -1192,9 +1209,12 @@ class GTEST_API_ UnitTest { ...@@ -1192,9 +1209,12 @@ class GTEST_API_ UnitTest {
const std::string& os_stack_trace) const std::string& os_stack_trace)
GTEST_LOCK_EXCLUDED_(mutex_); GTEST_LOCK_EXCLUDED_(mutex_);
// Adds a TestProperty to the current TestResult object. If the result already // Adds a TestProperty to the current TestResult object when invoked from
// contains a property with the same key, the value will be updated. // inside a test, to current TestCase's ad_hoc_test_result_ when invoked
void RecordPropertyForCurrentTest(const char* key, const char* value); // from SetUpTestCase or TearDownTestCase, or to the global property set
// when invoked elsewhere. If the result already contains a property with
// the same key, the value will be updated.
void RecordProperty(const std::string& key, const std::string& value);
// Gets the i-th test case among all the test cases. i can range from 0 to // Gets the i-th test case among all the test cases. i can range from 0 to
// total_test_case_count() - 1. If i is not in that range, returns NULL. // total_test_case_count() - 1. If i is not in that range, returns NULL.
...@@ -1210,6 +1230,7 @@ class GTEST_API_ UnitTest { ...@@ -1210,6 +1230,7 @@ class GTEST_API_ UnitTest {
friend class internal::AssertHelper; friend class internal::AssertHelper;
friend class internal::ScopedTrace; friend class internal::ScopedTrace;
friend class internal::StreamingListenerTest; friend class internal::StreamingListenerTest;
friend class internal::UnitTestRecordPropertyTestHelper;
friend Environment* AddGlobalTestEnvironment(Environment* env); friend Environment* AddGlobalTestEnvironment(Environment* env);
friend internal::UnitTestImpl* internal::GetUnitTestImpl(); friend internal::UnitTestImpl* internal::GetUnitTestImpl();
friend void internal::ReportFailureInUnknownLocation( friend void internal::ReportFailureInUnknownLocation(
......
...@@ -348,8 +348,7 @@ class TestPropertyKeyIs { ...@@ -348,8 +348,7 @@ class TestPropertyKeyIs {
// Constructor. // Constructor.
// //
// TestPropertyKeyIs has NO default constructor. // TestPropertyKeyIs has NO default constructor.
explicit TestPropertyKeyIs(const char* key) explicit TestPropertyKeyIs(const std::string& key) : key_(key) {}
: key_(key) {}
// Returns true iff the test name of test property matches on key_. // Returns true iff the test name of test property matches on key_.
bool operator()(const TestProperty& test_property) const { bool operator()(const TestProperty& test_property) const {
...@@ -712,6 +711,12 @@ class GTEST_API_ UnitTestImpl { ...@@ -712,6 +711,12 @@ class GTEST_API_ UnitTestImpl {
ad_hoc_test_result_.Clear(); ad_hoc_test_result_.Clear();
} }
// Adds a TestProperty to the current TestResult object when invoked in a
// context of a test or a test case, or to the global property set. If the
// result already contains a property with the same key, the value will be
// updated.
void RecordProperty(const TestProperty& test_property);
enum ReactionToSharding { enum ReactionToSharding {
HONOR_SHARDING_PROTOCOL, HONOR_SHARDING_PROTOCOL,
IGNORE_SHARDING_PROTOCOL IGNORE_SHARDING_PROTOCOL
...@@ -1038,8 +1043,9 @@ bool ParseNaturalNumber(const ::std::string& str, Integer* number) { ...@@ -1038,8 +1043,9 @@ bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
class TestResultAccessor { class TestResultAccessor {
public: public:
static void RecordProperty(TestResult* test_result, static void RecordProperty(TestResult* test_result,
const std::string& xml_element,
const TestProperty& property) { const TestProperty& property) {
test_result->RecordProperty(property); test_result->RecordProperty(xml_element, property);
} }
static void ClearTestPartResults(TestResult* test_result) { static void ClearTestPartResults(TestResult* test_result) {
......
...@@ -57,7 +57,7 @@ else: ...@@ -57,7 +57,7 @@ else:
STACK_TRACE_TEMPLATE = '' STACK_TRACE_TEMPLATE = ''
EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?> EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests"> <testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
<testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*"> <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
<testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/> <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
</testsuite> </testsuite>
...@@ -97,7 +97,7 @@ Invalid characters in brackets []%(stack)s]]></failure> ...@@ -97,7 +97,7 @@ Invalid characters in brackets []%(stack)s]]></failure>
<testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*"> <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
<testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/> <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
</testsuite> </testsuite>
<testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*"> <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*" SetUpTestCase="yes" TearDownTestCase="aye">
<testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/> <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
<testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/> <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
<testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/> <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
......
...@@ -95,6 +95,9 @@ TEST(InvalidCharactersTest, InvalidCharactersInMessage) { ...@@ -95,6 +95,9 @@ TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
} }
class PropertyRecordingTest : public Test { class PropertyRecordingTest : public Test {
public:
static void SetUpTestCase() { RecordProperty("SetUpTestCase", "yes"); }
static void TearDownTestCase() { RecordProperty("TearDownTestCase", "aye"); }
}; };
TEST_F(PropertyRecordingTest, OneProperty) { TEST_F(PropertyRecordingTest, OneProperty) {
...@@ -120,12 +123,12 @@ TEST(NoFixtureTest, RecordProperty) { ...@@ -120,12 +123,12 @@ TEST(NoFixtureTest, RecordProperty) {
RecordProperty("key", "1"); RecordProperty("key", "1");
} }
void ExternalUtilityThatCallsRecordProperty(const char* key, int value) { void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
testing::Test::RecordProperty(key, value); testing::Test::RecordProperty(key, value);
} }
void ExternalUtilityThatCallsRecordProperty(const char* key, void ExternalUtilityThatCallsRecordProperty(const std::string& key,
const char* value) { const std::string& value) {
testing::Test::RecordProperty(key, value); testing::Test::RecordProperty(key, value);
} }
...@@ -173,5 +176,6 @@ int main(int argc, char** argv) { ...@@ -173,5 +176,6 @@ int main(int argc, char** argv) {
TestEventListeners& listeners = UnitTest::GetInstance()->listeners(); TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
delete listeners.Release(listeners.default_xml_generator()); delete listeners.Release(listeners.default_xml_generator());
} }
testing::Test::RecordProperty("ad_hoc_property", "42");
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }
...@@ -80,7 +80,9 @@ class GTestXMLTestCase(gtest_test_utils.TestCase): ...@@ -80,7 +80,9 @@ class GTestXMLTestCase(gtest_test_utils.TestCase):
actual_attributes = actual_node .attributes actual_attributes = actual_node .attributes
self.assertEquals( self.assertEquals(
expected_attributes.length, actual_attributes.length, expected_attributes.length, actual_attributes.length,
'attribute numbers differ in element ' + actual_node.tagName) 'attribute numbers differ in element %s:\nExpected: %r\nActual: %r' % (
actual_node.tagName, expected_attributes.keys(),
actual_attributes.keys()))
for i in range(expected_attributes.length): for i in range(expected_attributes.length):
expected_attr = expected_attributes.item(i) expected_attr = expected_attributes.item(i)
actual_attr = actual_attributes.get(expected_attr.name) actual_attr = actual_attributes.get(expected_attr.name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment