2013-11-01 74 views
1

我有gtest和運行使用代碼如下所示。我想將測試輸出打印到文本文件中,而不是在控制檯中顯示。有沒有辦法做到這一點?如何將Google測試輸出打印到文本文件?

我使用控制檯中的cmake運行測試:cmake CMakeLists.txt && make && ./runTests

#include "cw-test.c" 
#include <stdio.h> 
#include <gtest/gtest.h> 

TEST(InputValidationTest, ValidateEntryLine) 
{ 
    ... 
} 

... 

int main(int argc, char **argv) { 
    testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 
+0

這個工作適合你:'cmake CMakeLists.txt && make && ./runTests&> test_output.txt'(參見[this](http://code.google.com/p/googletest/wiki/) FAQ#The_Google_Test_output_is_buried_in_a_whole_bunch_of_log_message))? – crayzeewulf

+0

@crayzeewulf是的!很好,謝謝。把它放在答案中,並得到一些觀點。它適用於任何程序還是與gtest有關? – RobotEyes

+0

RobotEyes,完成。此外,該類型的[重定向](http://en.wikipedia.org/wiki/Redirection_(計算))將適用於您在shell中運行的任何命令。 – crayzeewulf

回答

1

您可以將runTests命令的輸出重定向到一個文件:

cmake CMakeLists.txt && make && ./runTests > test_output.txt 

而且,看到this這可以解釋爲什麼你不需要我在我的評論中使用的&。正如Awaken的回答所說,&stdoutstderr重定向到同一個文件。但由於googletest輸出始終爲stdout,因此您可以省略&

+2

但是被測試的代碼本身可能會記錄到stderr。如果僅使用>,那些打印輸出將不在輸出文件中。 – VladLosev