Внедрение исключения с помощью буст-теста - PullRequest
0 голосов
/ 03 октября 2019

Я только начал изучать фреймворк для буст-теста и gmock. Я хочу провести отрицательное тестирование с помощью буст-теста. Есть ли способ, которым я могу сказать, чтобы при повышении теста было вызвано исключение при вызове моей функции? Я читал, что gmock позволяет определять поведение, чтобы можно было выполнять как положительное, так и отрицательное тестирование, но я не знаю, как эмулировать выбрасывание исключения, используя boost.

Код выглядит примерно так.

Это мой тестовый код.

#define BOOST_TEST_MODULE Test_MyInterface
// Some includes here

class MyInterfaceTest {
public:
  MyInterfaceTest(void) {
    // Initializes some data for testing
  }
}

BOOST_FIXTURE_TEST_SUITE(Test_This_Module, MyInterfaceTest)

BOOST_AUTO_TEST_CASE(GetData) 
{

  DInterface d_interface;

  // Test1 - Check that GetData does not throw exception
  BOOST_REQUIRE_NO_THROW(d_interface.GetData(false));

  //Test2 - Check that GetData throws exception

  /* This is where I have problem with testing. 
  How do I make GetData() throw an exception using boost test framework?
  GetData() calls GetMyStream(2) and I don't see GetMyStream(2) throwing an exception in the normal scenario
  */
}

Мой реальный код выглядит примерно так

class BInterface {
public:
  virtual ~BInterface() = 0;
  virtual  bool GetData(
bool IsIncluded) const = 0;
}

class DInterface : public BInterface {
public:
  ~DInterface();
  bool GetData(
bool Consider) const;

private: 
  GetMyStream(int StreamType) const;
}

bool DInterface::GetData(bool IsIncluded) const
{
  bool result = true;
  try {

    bool result;
    result = GetMyStream(2);
  }
  catch {
     throw;  // rethrows an exception from GetMyStream() call
  }
  return result;
}

bool DInterface::GetMyStream(int number) const
{
  // Code is something like this

  if  open() succeeds
    return true;
  else 
    throw std::exception;

  return true;
}
...