804*_*142 -1 c++ oop static cppunit initialization
我正在尝试使用CppUnit来测试一个只在第一次调用时才执行某些代码的方法.
class CElementParseInputTests: public CppUnit::TestFixture {
private:
CElement* element;
public:
void setUp() {
element = new CElement();
}
void tearDown() {
delete element;
}
void test1() {
unsigned int parsePosition = 0;
CPPUNIT_ASSERT_EQUAL(false, element->parseInput("fäil", parsePosition));
}
void test2() {
unsigned int parsePosition = 0;
CPPUNIT_ASSERT_EQUAL(false, element->parseInput("pass", parsePosition));
}
Run Code Online (Sandbox Code Playgroud)
我想测试的递归方法:
bool CElement::parseInput(const std::string& input, unsigned int& parsePosition) {
static bool checkedForNonASCII = false;
if(!checkedForNonASCII) {
std::cout << "this should be printed once for every test case" << std::endl;
[...]
checkedForNonASCII = true;
}
[...]
parseInput(input, parsePosition+1)
[...]
}
Run Code Online (Sandbox Code Playgroud)
由于对象是为每个测试用例重新创建然后销毁的,我希望在运行测试时,字符串"这应该为每个测试用例打印一次"将打印两次,但它只打印一次.我错过了什么?