2011-12-14 3 views

risposta

17

Non ci sono combinazioni pronte per l'uso di test parametrizzati per tipo e test parametrici. Gli sviluppatori googletest sono stati asked the question and they said No.

Tuttavia, c'è un modo di routine e semplice (come suggerito da Zhanyong Wan nella discussione linked) in cui è possibile craftare si possiede tipo-parametrizzate test che verifica una certa condizione per un determinato intervallo dei valori del tipo di parametro. Ecco un esempio elementare, dove la condizione è solo è maggiore di 0:

#include <vector> 
#include "gtest/gtest.h" 

template<class T> 
struct foo_test : public ::testing::Test { 
    static std::vector<T> _range_; 
}; 

TYPED_TEST_CASE_P(foo_test); 

TYPED_TEST_P(foo_test, IsGreaterThanZero) { 
    for (TypeParam value : foo_test<TypeParam>::_range_) { 
     EXPECT_GT(value,0); 
    } 
} 

REGISTER_TYPED_TEST_CASE_P(foo_test,IsGreaterThanZero); 

typedef ::testing::Types<char, int, float> MyTypes; 
INSTANTIATE_TYPED_TEST_CASE_P(My, foo_test, MyTypes); 

template<> std::vector<char> foo_test<char>::_range_{'1','2','3'}; 
template<> std::vector<int> foo_test<int>::_range_{1,2,3}; 
template<> std::vector<float> foo_test<float>::_range_{1.1,2.2,0.0}; 

int main(int argc, char **argv) { 
    ::testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

Quando compilato ed eseguito l'uscita di questo è:

[==========] Running 3 tests from 3 test cases. 
[----------] Global test environment set-up. 
[----------] 1 test from My/foo_test/0, where TypeParam = char 
[ RUN  ] My/foo_test/0.IsGreaterThanZero 
[  OK ] My/foo_test/0.IsGreaterThanZero (0 ms) 
[----------] 1 test from My/foo_test/0 (0 ms total) 

[----------] 1 test from My/foo_test/1, where TypeParam = int 
[ RUN  ] My/foo_test/1.IsGreaterThanZero 
[  OK ] My/foo_test/1.IsGreaterThanZero (0 ms) 
[----------] 1 test from My/foo_test/1 (0 ms total) 

[----------] 1 test from My/foo_test/2, where TypeParam = float 
[ RUN  ] My/foo_test/2.IsGreaterThanZero 
/home/imk/develop/SO/gtest/main.cpp:14: Failure 
Expected: (value) > (0), actual: 0 vs 0 
[ FAILED ] My/foo_test/2.IsGreaterThanZero, where TypeParam = float (0 ms) 
[----------] 1 test from My/foo_test/2 (1 ms total) 

[----------] Global test environment tear-down 
[==========] 3 tests from 3 test cases ran. (1 ms total) 
[ PASSED ] 2 tests. 
[ FAILED ] 1 test, listed below: 
[ FAILED ] My/foo_test/2.IsGreaterThanZero, where TypeParam = float 

1 FAILED TEST 

I risultati hanno grossolana granularità quanto sarebbe ideale: solo 3 test, invece di di 9. Tuttavia, i valori in errore possono essere riportati, come mostrato, in modo che la grana grossa possa essere tollerabile .

+0

Qualche idea su come ottenere un prodotto cartesiano di tipo/valore parametri senza hard-coding esso? – memecs

+0

Per essere abbastanza chiari, si desidera testare ciascuno dei tipi 'T0, .., Tn' per ciascuno di un singolo insieme di valori' V0, .., Vm' tale che un 'Ti' è costruibile da' Vj' per any 'i <= n' e' j <= m'. È giusto? –

+0

Giusto, non sono sicuro di cosa intendi con "Ti costruibile da Vj". Ti darò un esempio per essere più chiaro: supponiamo di avere tre tipi {T1, T2, T3} e due parametri di valore {V1, V2}, voglio testare le seguenti tuple: {(T1, V1), (T1, V2), (T1, V3), (T2, V1), (T2, V2), ecc ...}. Il punto è che ho molti parametri e tipi da testare e non riesco a codificarli tutti. – memecs