2013-01-07 20 views
7

Ho un caso di test boost. La maggior parte delle linee di questo test case vengono eseguite indipendentemente dai parametri. Ma ci sono parti che vengono eseguite in base al parametro fornito. Voglio evitare di scrivere due casi di test separati che sono quasi identici tranne che in alcune parti minori. Così ho bisogno di usare qualcosa come il seguente approccio per creare casi di test con parametri:Test Boost: Come scrivere casi di test parametrizzati

BOOST_FIXTURE_TEST_CASE(caseA, Fixture) 
{ 
    TestFunction("parameterA"); 
} 

BOOST_FIXTURE_TEST_CASE(caseB, Fixture) 
{ 
    TestFunction("parameterB"); 
} 

void TestFunction(string param) 
{ 
    // ... 
    // lots of common checks regardless of parameters 
    // ... 
    if(param == "parameterA") 
     BOOST_CHECK(...); 
    else if(param == "parameterB") 
     BOOST_CHECK(...); 
} 

C'è un altro modo per raggiungere il mio obiettivo in un modo più conveniente? Potrei trovare la macro BOOST_PARAM_CLASS_TEST_CASE ma non sono sicuro se sia rilevante in questo caso.

+1

[Questa risposta] (http://stackoverflow.com/a/8110228/1252091) potrebbe esserti utile (non ho testato il codice). –

+0

possibile duplicato di [E 'possibile utilizzare BOOST \ _PARAM \ _TEST \ _CASE con la registrazione automatica su boost :: test?] (Http://stackoverflow.com/questions/8084038/is-it-possible-to-use- spinta-param-test-case-con-automatico-registrazione-on-boost) –

risposta

1

Nessun supporto Boost per quanto ne so, in modo da fare questo:

void test_function(parameters...) 
{ 
    <test code> 
} 

BOOST_AUTO_TEST_CASE(test01) { 
    test_function(parameters for case #1) 
} 

BOOST_AUTO_TEST_CASE(test02) { 
    test_function(parameters for case #2) 
} 

Si può fare con i modelli se li gradite:

template<int I, bool B> 
void test_function() 
{ 
    for(int i=0; i<I; i++) 
     if (B) BOOST_REQUIRE(i<10); 
} 

BOOST_AUTO_TEST_CASE(test01) { 
    test_function<10, true>(); 
} 

BOOST_AUTO_TEST_CASE(test02) { 
    test_function<20, false>(); 
} 
Problemi correlati