{"id":164994,"date":"2020-10-02T01:58:05","date_gmt":"2020-10-01T17:58:05","guid":{"rendered":"http:\/\/4563.org\/?p=164994"},"modified":"2020-10-02T01:58:05","modified_gmt":"2020-10-01T17:58:05","slug":"%e8%af%b7%e6%95%99-c%e5%a6%82%e4%bd%95%e6%b5%8b%e8%af%95%e7%94%a8%e6%88%b7%e7%9a%84%e8%be%93%e5%85%a5%ef%bc%8c-input-testing","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=164994","title":{"rendered":"\u8bf7\u6559 c++\u5982\u4f55\u6d4b\u8bd5\u7528\u6237\u7684\u8f93\u5165\uff0c input testing"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559 c++\u5982\u4f55\u6d4b\u8bd5\u7528\u6237\u7684\u8f93\u5165\uff0c input testing               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>C++\u5c0f\u767d\uff0c\u6700\u8fd1\u5199\u7684\u5c0f demo \u7528\u5230\u4e86 Gtest \u7684 unit test \u5927\u591a\u6570\u7684 function<\/p>\n<p>\u4f46\u662f\u5f53\u4e3b\u51fd\u6570\u6839\u636e\u7528\u6237\u7684\u8f93\u5165\uff08 cin \uff09\u6765\u4f20\u503c\u7ed9 function \u7684\u65f6\u5019\u5982\u4f55\u6d4b\u8bd5\u5462\uff1f<\/p>\n<p>\u6682\u65f6\u60f3\u5230\u7684\u65b9\u6cd5\u5c31\u662f\u628a\u9700\u8981\u8f93\u5165\u7684\u5185\u5bb9\u7528 txt \u6587\u4ef6\u8fdb\u884c\u4fdd\u5b58\uff0c\u7136\u540e\u7528.\/executable &lt; input.txt \u7684\u65b9\u5f0f\u8fdb\u884c\u6bd4\u6a21\u62df\u7528\u6237\u8f93\u5165\u3002<\/p>\n<p>\u5927\u6982\u6d4b\u8bd5\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<p>https:\/\/stackoverflow.com\/questions\/11408020\/c-testing-with-input-cases-from-text-files<\/p>\n<p>\u7136\u540e\u5199\u6210 shell \u811a\u672c\uff1a<\/p>\n<pre><code>EXEFile=$(find \"$(pwd -P)\"  -name 'GTestSelfInput' | head -1)  cd $CURRENT_DIR  $EXEFile --gtest_filter=People_Services_input.initPassword01 &lt; input1.txt  if [ $? != \"0\" ]; then     exit 1 fi <\/code><\/pre>\n<p>\u7136\u540e\u901a\u8fc7\u4e0d\u540c\u6a21\u62df\u7684 input.txt \u4f20\u5165\u7684\u53c2\u6570\u518d\u4f7f\u7528 Gtest \u53bb\u786e\u8ba4\u64cd\u4f5c\u662f\u5426\u6210\u529f\u3002<\/p>\n<pre><code>int main(int argc, char **argv) {     ::testing::InitGoogleTest(&amp;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-&gt;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;  } <\/code><\/pre>\n<p>\u4f46\u662f\u8fd9\u6837\u6709\u5f88\u5927\u7684\u5c40\u9650\u6027\uff0c\u6bd4\u5982\u6ca1\u529e\u6cd5\u968f\u673a\u751f\u6210\u8f93\u5165\u5185\u5bb9\uff0c\u4f7f\u7528\u8d77\u6765\u4e5f\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002<\/p>\n<p>\u6709\u6ca1\u6709\u4ec0\u4e48\u6d4b\u8bd5\u6846\u67b6\u53ef\u4ee5\u89e3\u51b3\u8fd9\u6837\u7684\u95ee\u9898\u5462\uff1f\uff08 system testing\uff0cfunctional testing \uff09<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3627226\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lcdtyph <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f\u7528 std::cin \u505a\u8f93\u5165\u7684\u8bdd\u53ef\u4ee5\u8986\u76d6\u5b83\u7684 rdbuf<\/p>\n<p>&#8220;`<br \/>#include &lt;iostream&gt;<br \/>#include &lt;sstream&gt;<\/p>\n<p>int main() {<\/p>\n<p> std::string fake_input = &#8220;abc&#8221;;<br \/> std::istringstream iss{fake_input};<\/p>\n<p> std::cin.rdbuf(iss.rdbuf());<\/p>\n<p> std::string a;<br \/> std::cin &gt;&gt; a;<br \/> std::cout &lt;&lt; a &lt;&lt; std::endl;<\/p>\n<p> return 0;<br \/>}<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3627227\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5e94\u8be5\u9488\u5bf9 stream \u505a\u6d4b\u8bd5\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u7406\u4f1a\u662f\u6807\u51c6\u8f93\u5165\u6d41\u8fd8\u662f\u6587\u4ef6\u6d41\u8fd8\u662f\u5176\u4ed6\u4ec0\u4e48\u6d41                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3627228\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @reus \u600e\u4e48\u51c6\u961f stream \u505a\u6d4b\u8bd5\u5462\uff0c\u5927\u4f6c\u7ed9\u4e2a\u60f3\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559 c++\u5982\u4f55\u6d4b\u8bd5\u7528\u6237\u7684\u8f93\u5165\uff0c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/164994"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=164994"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/164994\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=164994"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=164994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}