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?
Writing unit tests with QTestLib
1.3k Views Asked by Stefano At
1
There are 1 best solutions below
Related Questions in C++
- C++ using std::vector across boundaries
- Linked list without struct
- Connecting Signal QML to C++ (Qt5)
- how to get the reference of struct soap inherited in C++ Proxy/Service class
- Why we can't assign value to pointer
- Conversion of objects in c++
- shared_ptr: "is not a type" error
- C++ template using pointer and non pointer arguments in a QVector
- C++ SFML 2.2 vectors
- Lifetime of temporary objects
- I want to be able to use 4 different variables in a select statement in c ++
- segmentation fault: 11, extracting data in vector
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- How can I print all the values in this linked list inside a hash table?
- Configured TTL for A record(s) backing CNAME records
Related Questions in UNIT-TESTING
- How to write tests for classes with inheritance
- PHPunit call magic methods
- Convert IEnumerable to IObservable with variable Period
- How to a run specific code before & after each unit test in Python
- Unit testing a class A that derives from an abstract class B
- Is there another way to unit test business logic in mvc
- Some of my tests show prepended with junit.framework
- Selenium Driver Service not found exception
- Can not convert from Class<PowerMockRunner> to Class<? extends Runner>
- AngularJS Unit Testing - multiple mocks and providers
- How to use Sinon.js FakeXMLHttpRequest with superagent?
- Get Mock with AutoMock.Create<>()
- Mock service that takes unitOfWork in constructor
- Cleanest method for creating a pytest test fixture that takes in dynamic text data
- g++ 5.1.0 not building project, clang shows unknown error
Related Questions in QT4
- Why scrollContentsBy(dx, dy) function not called when scroll by moving the cursor in QPlainTextEdit
- How to grab video frames in Qt?
- PySide: How to append text from different classes in QTextBrowser?
- Using Qmake in Makefile?
- Making QMessageBox InformativeText Bold and increase font size
- Show name of song on Qlabel
- qt designer qgraphicsview load image
- Qt4: Window appears in the upper left corner on metacity
- Qt application changes font when run as root
- How to include a lib with dependencies to QT
- Missing mocinclude.tmp
- Qt4 C++ Resize window, preserve square form
- Disconnect slot from parent class QAbstractItemView
- space between two tool buttons and hboxlayout
- Qt4 QTreeWidget with VS2015 x64 win10
Related Questions in QTESTLIB
- Access native QFileDialog
- How can I control when my gmock is verified?
- QTest mouseClick on a QPushButton
- How to create dependency injection for testing QTcpSocket?
- Simplest way to click a button with QTest
- How to run QTQuick test
- comparing QTest with other frameworks
- How can i terminate a delay / wait condition
- QTest::mouseClick RightButton can not work, can someone give some tips?
- QSignalSpy can not be used with threads
- How to get the information from QTestLib/QTest on test failure
- Problems with QTest::mouseClick on QListWidget
- How to run multiple tests in Qt without calling QTest::qExec multiple times?
- How to use foreach loop on a QSignalSpy
- Simple qtest fails with 'multiple definition of main'
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
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
QFileinstance 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 aQFile(a.k.a. a Test Double of aQFile).The easiest way to resolve this is to pass a
QIODeviceinto your class constructor (QFileinherits fromQIODevice). In your unit test, you can fake the contents of the file by passing in aQIODevicewith 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:Your unit test constructs the class with a
QBuffer; your production code constructs it with aQFile.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.