Catch失败就简单的例子而言

时间:2017-08-28 02:57:34

标签: c++ c++11 catch-unit-test

我正在尝试将Catch单元测试集成到我的项目中,但目前可用的

失败了
Catch v1.10.0
Generated: 2017-08-26 15:16:46.676990

示例:

test.cpp

#include "catch.hpp"

#define CATCH_CONFIG_MAIN


TEST_CASE("CATCH TEST"){
    REQUIRE(1 == 1);
}
g++ test.cpp -o test.exe -std=c++11

错误:

Undefined symbols for architecture x86_64:
  "Catch::ResultBuilder::endExpression(Catch::DecomposedExpression const&)", referenced from:
      Catch::BinaryExpression<int const&, (Catch::Internal::Operator)0, int const&>::endExpression() const in test-b77427.o
  "Catch::ResultBuilder::setResultType(bool)", referenced from:
      Catch::BinaryExpression<int const&, (Catch::Internal::Operator)0, int const&>::endExpression() const in test-b77427.o
  "Catch::ResultBuilder::useActiveException(Catch::ResultDisposition::Flags)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::ResultBuilder::react()", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::ResultBuilder::ResultBuilder(char const*, Catch::SourceLineInfo const&, char const*, Catch::ResultDisposition::Flags, char const*)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::ResultBuilder::~ResultBuilder()", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::SourceLineInfo::SourceLineInfo(char const*, unsigned long)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
      ___cxx_global_var_init in test-b77427.o
  "Catch::isDebuggerActive()", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "Catch::AutoReg::AutoReg(void (*)(), Catch::SourceLineInfo const&, Catch::NameAndDesc const&)", referenced from:
      ___cxx_global_var_init in test-b77427.o
  "Catch::AutoReg::~AutoReg()", referenced from:
      ___cxx_global_var_init in test-b77427.o
  "Catch::toString(int)", referenced from:
      Catch::ExpressionLhs<int const&>::reconstructExpression(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const in test-b77427.o
      Catch::BinaryExpression<int const&, (Catch::Internal::Operator)0, int const&>::reconstructExpression(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const in test-b77427.o
  "Catch::ResultBuilder::shouldDebugBreak() const", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test-b77427.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

1 个答案:

答案 0 :(得分:5)

目前,您的程序没有private int Question_no=0; private Boolean Boolean_Var=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_question1); String[] Question_Array = getResources().getStringArray(R.array.Question1); TextView Questions = (TextView) findViewById(R.id.Question); Questions.setText(Question_Array[Question_no]); String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1); RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1); Radio_Button1.setText(Radio_Button1_Array[Question_no]); String[] Radio_Button2_Array = getResources().getStringArray(R.array.Option_2); RadioButton Radio_Button2 = (RadioButton) findViewById(R.id.radioButton2); Radio_Button2.setText(Radio_Button2_Array[Question_no]); findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE); findViewById(R.id.NextButton).setVisibility(View.INVISIBLE); } public void On_RadioButton1_Click (View view) { if (Boolean_Var == false) { String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1); String CorrectAns = CorrectAns_Array[Question_no]; String[] Answer_Array = getResources().getStringArray(R.array.Option_1); String Answer = Answer_Array[Question_no]; if (Answer.equals(CorrectAns)) { RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1); Right_Ans.setTextColor(Color.GREEN); AnswerSubmitted(); } else { RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1); Wrong_Ans.setTextColor(Color.RED);; GreenTick(); AnswerSubmitted(); } } Boolean_Var = true; } public void On_RadioButton2_Click(View view) { if(Boolean_Var==false) { String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1); String CorrectAns = CorrectAns_Array[Question_no]; String[] Answer_Array = getResources().getStringArray(R.array.Option_2); String Answer = Answer_Array[Question_no]; if(Answer.equals(CorrectAns)) { RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton2); Right_Ans.setTextColor(Color.GREEN); } else { RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton2); Wrong_Ans.setTextColor(Color.RED); GreenTick(); } } Boolean_Var=true; AnswerSubmitted(); } public void GreenTick() { String[] Answer1_Array = getResources().getStringArray(R.array.Option_1); String Answer1 = Answer1_Array[Question_no]; String[] Answer2_Array = getResources().getStringArray(R.array.Option_2); String Answer2 = Answer2_Array[Question_no]; String[] Answer3_Array = getResources().getStringArray(R.array.Option_3); String Answer3 = Answer3_Array[Question_no]; String[] The_CorrectAns_Array = getResources().getStringArray(R.array.Answer1); String The_CorrectAnsIs = The_CorrectAns_Array[Question_no]; if(The_CorrectAnsIs.equals(Answer1)) { Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton1); Option_with_CorrectAns.setTextColor(Color.GREEN); } else if(The_CorrectAnsIs.equals(Answer2)) { Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton2); Option_with_CorrectAns.setTextColor(Color.GREEN); } else if(The_CorrectAnsIs.equals(Answer3)) { Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton3); Option_with_CorrectAns.setTextColor(Color.GREEN); } else { Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton4); Option_with_CorrectAns.setTextColor(Color.GREEN); } } public void AnswerSubmitted() { findViewById(R.id.MenuButton).setVisibility(View.VISIBLE); findViewById(R.id.NextButton).setVisibility(View.VISIBLE); } } 功能。如果编译它,链接器会抱怨缺少main函数。由于您使用catch框架编写单元测试,因此您希望该框架为您生成main函数。

catch框架查看是否定义了main,如果是,则为您生成CATCH_CONFIG_MAIN函数,该函数运行您定义的所有测试。由于编译是一个线性过程,因此宏main必须在之前定义,包括CATCH_CONFIG_MAIN。否则,库将永远不会“看到”此宏。

catch.hpp

Live example