我能知道gtest EXPECT_NO_Trow(或ASSERT_NO_Trow)中抛出了哪个异常吗? 测试我的C++项目,我使用GoogleTestFrase. 通常,我可以使用以下语法轻松调试故障: EXPECT_TRUE(*statement*) << *debugMessage*; EXPECT\u TRUE(*语句*)

我能知道gtest EXPECT_NO_Trow(或ASSERT_NO_Trow)中抛出了哪个异常吗? 测试我的C++项目,我使用GoogleTestFrase. 通常,我可以使用以下语法轻松调试故障: EXPECT_TRUE(*statement*) << *debugMessage*; EXPECT\u TRUE(*语句*),c++,exception,testing,automated-tests,googletest,C++,Exception,Testing,Automated Tests,Googletest,这里有一种方法: #include <exception> #include <stdexcept> #include <ostream> #include <iostream> // for the test #include <gtest/gtest.h> namespace detail { struct unwrapper { unwrapper(std::exception_ptr pe) :

这里有一种方法:

#include <exception>
#include <stdexcept>
#include <ostream>
#include <iostream> // for the test
#include <gtest/gtest.h>
namespace detail {

    struct unwrapper
    {
        unwrapper(std::exception_ptr pe) : pe_(pe) {}

        operator bool() const {
            return bool(pe_);
        }

        friend auto operator<<(std::ostream& os, unwrapper const& u) -> std::ostream&
        {
            try {
                std::rethrow_exception(u.pe_);
                return os << "no exception";
            }
            catch(std::runtime_error const& e)
            {
                return os << "runtime_error: " << e.what();
            }
            catch(std::logic_error const& e)
            {
                return os << "logic_error: " << e.what();
            }
            catch(std::exception const& e)
            {
                return os << "exception: " << e.what();
            }
            catch(...)
            {
                return os << "non-standard exception";
            }

        }
        std::exception_ptr pe_;
    };

}

auto unwrap(std::exception_ptr pe)
{
    return detail::unwrapper(pe);
}


template<class F>
::testing::AssertionResult does_not_throw(F&& f)
         {
             try {
                 f();
                 return ::testing::AssertionSuccess();
             }
             catch(...) {
                 return ::testing::AssertionFailure() << unwrap(std::current_exception());
             }
         };


TEST(a, b)
{
    ASSERT_TRUE(does_not_throw([] { throw std::runtime_error("i threw"); }));
}

Richard Hodges回答的另一种方法是在测试体内使用try-catch结构。这个解决方案来自杰夫·兰格(Jeff Langr)写的一本好书

完整的工作示例如下所示:

#include <stdexcept>
#include "gtest/gtest.h"

struct foo
{
  void bar() {
    throw std::runtime_error("unexpected error");
  }
};

TEST(foo_test, does_not_throw)
{
  foo f;
  try {
    f.bar();
    SUCCEED();
  }
  catch (std::exception const & err) {
    FAIL() << err.what();
  }
}

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

我参加聚会有点晚了,但有一种方法是有效的:

/**
*@brief使用try-catch包装代码块,处理抛出的异常,打印它们
*进入溪流并重新冲洗。
*/

#定义PRINT_和RETHROW(CODE_BLOCK,EXCEPT_STREAM)try{do{CODE_BLOCK}while(0);}catch(const std::exception&ex){EXCEPT_STREAM我同意。这也可以在助手函数或更简单的宏中轻松定义,以保持单行结构
#include <stdexcept>
#include "gtest/gtest.h"

struct foo
{
  void bar() {
    throw std::runtime_error("unexpected error");
  }
};

TEST(foo_test, does_not_throw)
{
  foo f;
  try {
    f.bar();
    SUCCEED();
  }
  catch (std::exception const & err) {
    FAIL() << err.what();
  }
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from foo_test
[ RUN      ] foo_test.does_not_throw
    /Users/Soeren/Documents/cmakeProject/src/applications/modelTest/main.cpp(26): error: Failed
unexpected error messages
[  FAILED  ] foo_test.does_not_throw (1 ms)
[----------] 1 test from foo_test (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (3 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] foo_test.does_not_throw

 1 FAILED TEST
Running main() from /build/googletest-qXr8Ln/googletest-1.10.0/googletest/src/gtest_main.cc
Note: Randomizing tests' orders with a seed of 60645 .
[==========] Running 2 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 1 test from test
[ RUN      ] test.test
std::exception thrown: this should be printed
/workspace/libasync/test/ut_executor_factory.cpp:56: Failure
Expected: try{do{ f(); }while(0);}catch(const std::exception& ex){ std::cerr << "std::exception thrown: " << ex.what() << std::endl; throw; }catch(...){ std::cerr << "unknown structure thrown" << std::endl; throw;} doesn't throw an exception.
  Actual: it throws.
[  FAILED  ] test.test (0 ms)
[----------] 1 test from test (0 ms total)