Google Test
Settting
Testing Standard Output
testing::internal::CaptureStdout();
std::cout << e;
std::string output = testing::internal::GetCapturedStdout();
Testing Standard Input
Date A(2018, 10, 18);
Date B = A;
//std::streambuf* orig = std::cin.rdbuf();
std::istringstream input("2018/10/18");
std::cin.rdbuf(input.rdbuf());
std::cin >> B;
ASSERT_TRUE(equalDates(A, B));
Testing Private Method
Setting Eclipse to Run Multiple Binary
Example
#include <iostream>
#include <sstream>
#include "../src/PosApp.h"
#include "gtest/gtest.h"
namespace {
class PosAppTest: public ::testing::Test {
protected:
ict::PosApp *app;
// You can do set-up work for each test here.
PosAppTest() {
app = nullptr;
const char *fname = "posapp.txt";
const char *bname = "bill.txt";
initF(fname);
app = new ict::PosApp(fname, bname);
}
// You can do clean-up work that doesn't throw exceptions here.
virtual ~PosAppTest() {
delete app;
}
virtual void initF(const char *fname) {
std::fstream fout(fname, std::ios::out);
fout << "P,1234,Milk,3.99,0,1,2017/5/04" << std::endl
<< "N,3456,Paper Cups,5.99,1,38" << std::endl
<< "P,4567,Butter,4.56,1,9,2017/5/15" << std::endl
<< "P,1212,Salted Butter,5.99,0,108,2017/05/03" << std::endl
<< "N,1313,Paper Tissue,1.22,1,204" << std::endl
<< "P,5656,Honey,12.99,1,10,2017/05/15" << std::endl;
fout.close();
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
// Code here will be called immediately after the constructor (right before each test).
virtual void SetUp() {
//std::cout << "SetUp" << std::endl;
}
// Code here will be called immediately after each test (right before the destructor).
virtual void TearDown() {
}
// Objects declared here can be used by all tests in the test case for Foo.
};
TEST_F(PosAppTest, AddNonPerishableItem) {
std::string expect = "Item Entry:\nSku: Name:Price: Taxed: Quantity: Item added.\n\n";
std::istringstream input("8888\n\n8888\n10\ny\n1\n2019/12/01\n");
std::cin.rdbuf(input.rdbuf());
testing::internal::CaptureStdout();
app->runTest(3);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(expect, output);
}
TEST_F(PosAppTest, UpdateItemQuantity) {
testing::internal::CaptureStdout();
std::cout << "Please enter the SKU: "
<< "Name:\n"
<< "Milk\n"
<< "Sku: 1234\n"
<< "Price: 3.99\n"
<< "Price after tax: N/A\n"
<< "Quantity: 1\n"
<< "Total Cost: 3.99\n"
<< "Expiry date: 2017/05/04\n"
<< "Please enter the number of purchased items: "
<< "Updated!\n\n";
std::string expect = testing::internal::GetCapturedStdout();
std::istringstream input("1234\n1\n");
std::cin.rdbuf(input.rdbuf());
testing::internal::CaptureStdout();
app->runTest(4);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(expect, output);
}
TEST_F(PosAppTest, Exit) {
std::string expect = "Goodbye!\n";
std::istringstream input("0");
std::cin.rdbuf(input.rdbuf());
testing::internal::CaptureStdout();
app->runTest(0);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(expect, output);
}
} //namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
References