Witam,
mam problem z dojściem, dlaczego na wyjściu QProcessu otrzymuje takie a nie inne wyniki.. Pokazując to na przykładzie unittestu, który ma zwrócić wiadomość o niemożliwości utworzenia katalogu, gdyż takowy już istnieje..
void MyCmdLine::test_mkdir_with_error()
{
QString mydir = QDir::currentPath() + "/hello";
QDir().mkdir(mydir);
systemshell = new SystemShell();
systemshell->runProgram("/bin/sh");
QString cmd = "mkdir hello";
systemshell->writeAtProgram(cmd.toLatin1());
systemshell->closeChannel();
QSignalSpy spy(systemshell,SIGNAL(outputSignal(QByteArray)));
spy.wait();
qDebug() << "number of messages: " << spy.count() << " data: " << spy.at(0).count();
for (int i = 0; i<spy.count(); i++)
for (int j =0; j<spy.at(i).count(); j++)
data.append(qvariant_cast<QByteArray>(spy.at(0).at(0)));
systemshell->closeProcess();
QCOMPARE(QString(data), QString("mkdir: "));
data.clear();
systemshell->deleteLater();
QDir().rmdir(mydir);
}
Ten test 'zazwyczaj' wychodzi poprawie. Czemu zazwyczaj - bo czasami na wyjściu uzyskiwane są inne dane ..
Przykładowo:
- Start testing of SystemShellTest *********
Config: Using QtTest library 5.3.2, Qt 5.3.2
PASS : SystemShellTest::initTestCase()
QDEBUG : SystemShellTest::testExecuteCommandWithError() "mkdir: "
QDEBUG : SystemShellTest::testExecuteCommandWithError() number of messages: 1 data: 1
QDEBUG : SystemShellTest::testExecuteCommandWithError() "nie można utworzyć katalogu „hello”"
QDEBUG : SystemShellTest::testExecuteCommandWithError() ": Plik istnieje"
QDEBUG : SystemShellTest::testExecuteCommandWithError() "
"
PASS : SystemShellTest::testExecuteCommandWithError()
PASS : SystemShellTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
* Finished testing of SystemShellTest *********
Innym razem:
- Start testing of SystemShellTest *********
Config: Using QtTest library 5.3.2, Qt 5.3.2
PASS : SystemShellTest::initTestCase()
QDEBUG : SystemShellTest::testExecuteCommandWithError() "mkdir: " <-- samo mkdir
QDEBUG : SystemShellTest::testExecuteCommandWithError() number of messages: 1 data: 1
PASS : SystemShellTest::testExecuteCommandWithError()
PASS : SystemShellTest::testExecuteCommandWithError()
PASS : SystemShellTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
* Finished testing of SystemShellTest *********
Jeszcze innym razem:
- Start testing of SystemShellTest *********
Config: Using QtTest library 5.3.2, Qt 5.3.2
PASS : SystemShellTest::initTestCase()
..
Actual (QString(data)) : "mkdir: nie mo\u017Cna utworzy\u0107 katalogu \u201Ehello\u201D: Plik istnieje\n"
..
PASS : SystemShellTest::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
* Finished testing of SystemShellTest *********
Ogólnie, dlatego test 'zazwyczaj' wypada poprawnie ponieważ porównywaniu podlega string "mkdir: ", który jest oczekiwany w pierwszej kolejności. Domyślam się, że informacje z QProcessu mogą dochdzić 'różnie', bo to pewnie osobny wątek (tak?), ale wtedy jak mogę poprawnie wykonać ten unittest?
ps. Dorzucam jeszcze fragmenty klasy, w której jest logika QProcessu.. (m.in. gdzie wyrzucane są na konsole dane z QProcesu..).
bool SystemShell::runProgram(QString program, QStringList parameters)
{
m_Shell->start(program, parameters);
return m_Shell->waitForStarted();
}
void SystemShell::showOutput()
{
QByteArray output = m_Shell->readAllStandardOutput();
//m_Shell->close();
//#ifdef DEBUG
qDebug() << output;
//#endif
emit outputSignal(output);
}
void SystemShell::closeChannel()
{
m_Shell->closeWriteChannel();
//m_Shell->waitForFinished();
}
void SystemShell::closeProcess()
{
m_Shell->close();
}