2011-09-15 11 views
1

関連するコードを持っている述語を使用しようとすると、これはUnitTest++/TestRunner.hからである。ここではunittestの++の問題:状態

class TestRunner 
{ 
public: 
    explicit TestRunner(TestReporter& reporter); 
    ~TestRunner(); 

    template <class Predicate> 
    int RunTestsIf(TestList const& list, char const* suiteName, 
        const Predicate& predicate, int maxTestTimeInMs) const 
    { 
     Test* curTest = list.GetHead(); 

     while (curTest != 0) 
     { 
      if (IsTestInSuite(curTest,suiteName) && predicate(curTest)) 
      { 
       RunTest(m_result, curTest, maxTestTimeInMs); 
      } 

      curTest = curTest->next; 
     } 

     return Finish(); 
    } 

private: 
    TestReporter* m_reporter; 
    TestResults* m_result; 
    Timer* m_timer; 

    int Finish() const; 
    bool IsTestInSuite(const Test* const curTest, char const* suiteName) const; 
    void RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const; 
}; 

は、私はそれは私がやりたいようにする修正しようとしています私の述語クラスです:

class ListFilterRemember { 
    char **list; 
    int n; 
    Test **testsAlreadyRun; 
    int index_tar; 
    int max_allocd; 
public: 
    ListFilterRemember(char **list_, int count) { 
       int testCount = 0; 
     Test* curTest = Test::GetTestList().GetHead(); 
     while (curTest != 0) { 
      testCount++; 
     } 
       list = list_; n = count; max_allocd = testCount; 
     testsAlreadyRun = new Test *[max_allocd]; 
     index_tar = 0; 
    } 
    bool operator()(const Test* const t) const { 
     for (int i=0;i<index_tar;++i) { 
      if (testsAlreadyRun[i] == t) { return false; } 
     } 
     for (int i=0;i<n;++i) { 
      std::string dot_cpp_appended = std::string(list[i]) + ".cpp"; 
      if (!strcasecmp(t->m_details.testName, list[i]) || 
        !strcasecmp(t->m_details.suiteName, list[i]) || 
        !strcasecmp(t->m_details.filename, list[i]) || 
        !strcasecmp(t->m_details.filename, dot_cpp_appended.c_str()) || (
          filename_dir_prefix_len < (int)strlen(t->m_details.filename) && (// ensure the ptr arith in next 2 lines doesn't go out of bounds 
            !strcasecmp(t->m_details.filename+filename_dir_prefix_len, list[i]) || 
            !strcasecmp(t->m_details.filename+filename_dir_prefix_len, dot_cpp_appended.c_str()) 
          ) 
        ) || (
          std::string::npos != findCaseInsensitive(t->m_details.testName,list[i]) 
        ) 
      ) { 
       // erring on the side of matching more tests 
       //printf(" running\n"); 
       if (index_tar >= max_allocd) throw std::runtime_error("Did not allocate! Segfault here."); 
       testsAlreadyRun[index_tar] = (Test *)t; 
       index_tar += 1; 
       return true; 
      } 
     } 
     //printf(" not running\n"); 
     return false; 
    } 
    ~ListFilterRemember() { 
     delete[] testsAlreadyRun; 
    } 
}; 

TestRunner.hに定義されている方法を見ると、operator()関数がメンバー変数を変更できないようにするconst修飾子が付いています。これらの変更を加えて、すでに実行したテストを覚えて、再度実行しないようにする必要があります。彼らが再び動く危険性がある理由は、私がRunTestsIf()を何度も実行しようとしているからです。

コマンドラインでテストのリストを入力して、名前に基づいて実行するテストを指定します。これが、文字列にマッチするコードのすべてです。私はまだそれらを使用したいと思いますが、今度は、指定したテストが指定した順序で実行されるように改善したいと思います。これを行うには、テストランナーを移動して、指定されたテストリストを照合し、それらを1つずつ照合します。

アイデア?私はUnitTest ++コードでconstを核にしたいと思っていますが、私がする必要がなければ何かを壊したくありません。

答えて

1

キーワードはmutableというキーワードを使用できますが、テストランナーが同じ述部のインスタンスを再利用しない可能性があるため、テスト間で変更が失われる可能性があります。もちろん、投稿されたコードがテストランナー全体の場合は、毎回同じPredicateインスタンスを呼び出すように見えます。

+0

私は 'mutable'キーワードについて知りませんでした。それを指摘してくれてありがとう - それはほぼこのような状況のために作られているかのようです。私はいくつかのテストをして、Predicateで奇妙な動きがあるかどうかを調べる必要がありますが、srcと私が投稿したビットは '' Predicate ''への唯一の参照です。私はUnitTest ++のsrcを少し編集して**動作するようにすることもできますが、おそらく再コンパイルする必要があります。ちょっと...良くない。とりあえずありがとう。 –

0

通常、単体テストにはテストするための決定的なセットがあると考えられます。これは統合テストに近いかもしれません。それは必ずしも悪いことではありません。最初に小さなビットをテストしてください。

つまり、UnitTest ++は小さくて簡単に変更できるように設計されています。私は拡張するか、既存の機能を破ることはありませんが、あなたのニーズに合わせてそれをラップします。

+0

私の単位テスト実行ファイルをcmd行なしで実行すると、argsはすべてのテストを通常の方法で実行します。私はちょうど考えました、なぜ私はそれが欲しいことをやりませんか? –

関連する問題