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 8423057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:37:46+00:00 2026-06-10T03:37:46+00:00

this is my first step into the world on unit-testing so please be patient.

  • 0

this is my first step into the world on unit-testing so please be patient. I’m writing a Qt 4.8.1 C++ program and as test framework I’m using the integrated QTestLib. I should test a class that inhrerits from a base one and that loads a certain XML file to read and parse it later with a function that will return a C++ class object with the xml values that I can retrieve and set via set/get functions. The class in the constructor instantiate a QFile object and in the readXml function I use it to parse with the QXmlStreamReader the file. What’s the best way to test all this class with unit-tests? I don’t see how I could bypass the file-read operation to inject a own string representing the file content. Or should I use as test content a different ad-hoc XML file?

  • 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-10T03:37:48+00:00Added an answer on June 10, 2026 at 3:37 am

    Injecting a string to represent the file contents seems like it would be the most straightforward way of testing a class such as this. However, directly instantiating a QFile instance in your class constructor makes this impossible (in other words, it’s impossible to inject your dependency). Moreover, it’s quite a bit of work to create a “Fake” or “Mock” version of a QFile (a.k.a. a Test Double of a QFile).

    The easiest way to resolve this is to pass a QIODevice into your class constructor (QFile inherits from QIODevice). In your unit test, you can fake the contents of the file by passing in a QIODevice with the contents you wish to test. In this case, you can accomplish this with a QBuffer, which allows you to arbitrarily set its contents. As such, your class would like something like the following:

    class MyXmlParser
    {
       MyXmlParser(QIODevice* device);
    
       ...
    };
    

    Your unit test constructs the class with a QBuffer; your production code constructs it with a QFile.

    Should I use a real XML file in my test?

    Generally speaking, the more external dependencies your unit test has, the more costly it is to ensure that it doesn’t break in the future (it potentially makes your unit test sensitive to its context). As such, it is advisable to avoid using a real XML file in order to ensure that your test is self-contained. Moreover, passing in a real XML means that you are now implicitly testing QFile as well as your XML parser (in other words, it’s not longer a unit test, it’s an integration test). You can generally assume that the IO device that you pass into your parser works; you only need to verify that your parser uses the IO device correctly and that it can parse the XML properly.

    Consider reading through the material at xunitpatterns.com, particularly the section on Test Smells.

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

Sidebar

Related Questions

This is my first step into the world of stackoverflow, so apologies if I
I'm trying my first step into the magical world of Chrome Extensions. Now i've
This is my first step to programming.guys your help hands required.I watched youtube video
How to create in-app step by step instructions? Like this ( first boot android
Using Dozer to map two objects, I have: /** /* This first class uses
#include <iostream> using namespace std; // This first class contains a vector and a
Code first: '''this is main structure of my program''' from twisted.web import http from
I am starting my first foray into the world of Prism v4/MVVM with MEF
I have recently started learning java as my first ever step into object oriented
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo

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.