samedi 5 mars 2016

QT Test - Variables / Objects losing value

I have a QT Project Test.pro and want to test some classes in a few testclasses. Test.pro:

QT       += testlib serialport
QT       -= gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

FORMS    += ../Folder/mainwindow.ui

TARGET = main
CONFIG += console
CONFIG += testcase
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += \
    tst_var.cpp

DEFINES += SRCDIR=\\\"$$PWD/\\\"

#LIBS += -L../libs -lserial

HEADERS += \
    tst_var.h

FORMS    +=     ../Folder/mainwindow.ui \         
                ...

QT       +=     gui

header file tst_var.h:

#ifndef TST_VAR_H
#define TST_VAR_H

#include <QObject>
#include <QtTest/QtTest>
#include <QString>
#include <iostream>

using namespace std;

class tst_var: public QObject
{
    Q_OBJECT
public:
     tst_var();
private  slots:
    void initTestCase();
    void testGetName();
    void cleanupTestCase();
private:
    QByteArray varname;
};

#endif // TST_VAR_H

The Problem is that it does not matter where I define the variable varname it always loses his value. tst_var.cpp:

#include "tst_var.h"

tst_var::tst_var(){
    QByteArray varname("test");
    cout << "varname1:" << varname.constData() << endl;
    //Output: test
}

void tst_var::initTestCase(){
    //It does not make a different if I would define varname here  
}

void tst_var::testGetName(){
    cout << "varname2:" << varname.constData() << endl;
   //Output: (nothing), so varname lose its content
}

void tst_var::cleanupTestCase(){}

I start the test via main method. main.cpp:

#include <QtTest/QtTest>
#include "tst_var.h"

int main(int argc, char *argv[]) {
    tst_var var;
    QTest::qExec(&var);
}

So I do not know why the variable lose its value and how to fix it.

Aucun commentaire:

Enregistrer un commentaire