Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:02:24+00:00 2026-06-15T21:02:24+00:00

I need to make some unit test for my school project in Qt and

  • 0

I need to make some unit test for my school project in Qt and although I have read Qt tutorial on that I can’t figure out how am I supposed to write such tests. All of tests shown in tutorial I’ve mentioned refer to built-in methods. How should I write a unit test for a custom class say this is the simplest class I have:

task.h

#ifndef TASK_H
#define TASK_H

#include <QDateTime>
#include <QTime>

class Task
{
private:
    bool ifDone;
    QString name;
    QString description;
    QDateTime *startTime;
    QTime *start;
    QDateTime *endTime;
    QTime *end;
    bool neededReminder;
    QDateTime *reminderTime;

public:
    Task(QString _name, QString _description, QDate *dayClicked, 
         QTime *_startTime, QTime *_endTime, bool reminder);

    QString toString();
};

#endif // TASK_H    `

task.cpp

#include "task.h"

Task::Task(QString _name, QString _description, QDate *dayClicked, 
    QTime *_startTime, QTime *_endTime, bool reminder)
{
    ifDone = 0;
    name = _name;
    description = _description;
    start = _startTime;
    end = _endTime;
    startTime = new QDateTime(*dayClicked, *start);
    endTime = new QDateTime(*dayClicked, *end);
    neededReminder = reminder;
}

QString Task::toString() {
    QString task;
    task.append(this->name);
    task.append(" ");
    task.append(this->start->toString("HH:mm"));
    task.append(" - ");
    task.append(this->end->toString("HH:mm"));
    return task;
}

I was trying to #include this class to testing class as well as adding both .h and .cpp files to the project and I didn’t manage to do anything. Can anyone write some sample testing methods (for toString method and constructor) for the above class so I could carry on myself with the rest. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:02:25+00:00Added an answer on June 15, 2026 at 9:02 pm

    To be sincere, I think this question should be closed and that you should start reading some of the books you’ve been provided. Also, I don’t see what there is to test here, maybe the result of the string? However, see if this helps you:

    UnitTests.pro

    QT       += testlib
    QT       -= gui
    TARGET = tst_unitteststest
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    SOURCES += tst_unitteststest.cpp task.cpp
    DEFINES += SRCDIR=\\\"$$PWD/\\\"
    HEADERS += task.h
    

    tst_unitteststest.cpp

    #include <QString>
    #include <QtTest>
    #include "task.h"
    
    class UnitTestsTest : public QObject
    {
       Q_OBJECT
    
    public:
       UnitTestsTest();
    
    private Q_SLOTS:
       void initTestCase();
       void cleanupTestCase();
       void testCase1();
    };
    
    UnitTestsTest::UnitTestsTest()
    {
    }
    
    void UnitTestsTest::initTestCase()
    {
    }
    
    void UnitTestsTest::cleanupTestCase()
    {
    }
    
    void UnitTestsTest::testCase1()
    {
       QVERIFY2(true, "Failure");
       Task t("name", "desc", new QDate(1, 1, 2012), new QTime(0, 0), new QTime(1, 0), false);
       QVERIFY(t.toString() == "name 00:00 - 01:00");
    }
    QTEST_APPLESS_MAIN(UnitTestsTest)
    #include "tst_unitteststest.moc"
    

    Consider that there are mem leaks here… but I don’t know how you want your class to become. The test will pass here of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I need to unit test some scenarios that require
I have an excel file that I need to make some changes. I need
I need make some unit test or some kind of testing which make me
I need to create some unit test of my code that are working right
I have some data which i need to make some statistics. I need to
I need to make use of some OWL ontologies in c#. Does anyone have
I need make some action (dump statistical data) before the Dart program ends. The
I need to make some changes to an old Oracle stored procedure on an
I need to make some recurrent modification in a lot of files (C++). I
I need to make some part of text bold while displaying in jquery: $(':checked').each(function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.