跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 请教 c++如何测试用户的输入, input testing
未分類
2 10 月 2020

请教 c++如何测试用户的输入, input testing

请教 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)

  • 資深大佬 : lcdtyph

    如果是用 std::cin 做输入的话可以覆盖它的 rdbuf

    “`
    #include <iostream>
    #include <sstream>

    int main() {

    std::string fake_input = “abc”;
    std::istringstream iss{fake_input};

    std::cin.rdbuf(iss.rdbuf());

    std::string a;
    std::cin >> a;
    std::cout << a << std::endl;

    return 0;
    }
    “`

  • 資深大佬 : reus

    你应该针对 stream 做测试,这样就不需要理会是标准输入流还是文件流还是其他什么流

  • 主 資深大佬 : AkideLiu

    @reus 怎么准队 stream 做测试呢,大佬给个想法

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具