请教 c++如何测试用户的输入, input testing
資深大佬 : AkideLiu 2
C++小白,最近写的小 demo 用到了 Gtest 的 unit test 大多数的 function
但是当主函数根据用户的输入( cin )来传值给 function 的时候如何测试呢?
暂时想到的方法就是把需要输入的内容用 txt 文件进行保存,然后用./executable < input.txt 的方式进行比模拟用户输入。
大概测试思路如下:
https://stackoverflow.com/questions/11408020/c-testing-with-input-cases-from-text-files
然后写成 shell 脚本:
EXEFile=$(find "$(pwd -P)" -name 'GTestSelfInput' | head -1) cd $CURRENT_DIR $EXEFile --gtest_filter=People_Services_input.initPassword01 < input1.txt if [ $? != "0" ]; then exit 1 fi
然后通过不同模拟的 input.txt 传入的参数再使用 Gtest 去确认操作是否成功。
int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } //init password successes TEST(People_Services_input, initPassword01) { Storage::setSUserId(1); EXPECT_TRUE(PeopleServices::initPassword()); People *p = PeopleDao::selectOnePeople(1); EXPECT_EQ(p->getPassword(), "password123"); mysql::connection db = database_connection::getConnection(); db.execute("UPDATE oop.people t SET t.password = '-1' WHERE t.user_id = 1;"); delete p; }
但是这样有很大的局限性,比如没办法随机生成输入内容,使用起来也非常不方便。
有没有什么测试框架可以解决这样的问题呢?( system testing,functional testing )
大佬有話說 (3)