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++
- How to immediately apply DISPLAYCONFIG_SCALING display scaling mode with SetDisplayConfig and DISPLAYCONFIG_PATH_TARGET_INFO
- Why can't I use templates members in its specialization?
- How to fix "Access violation executing location" when using GLFW and GLAD
- Dynamic array of structures in C++/ cannot fill a dynamic array of doubles in structure from dynamic array of structures
- How do I apply the interface concept with the base-class in design?
- File refuses to compile std::erase() even if using -std=g++23
- How can I do a successful map when the number of elements to be mapped is not consistent in Thrust C++
- Can std::bit_cast be applied to an empty object?
- Unexpected inter-thread happens-before relationships from relaxed memory ordering
- How i can move element of dynamic vector in argument of function push_back for dynamic vector
- Brick Breaker Ball Bounce
- Thread-safe lock-free min where both operands can change c++
- Watchdog Timer Reset on ESP32 using Webservers
- How to solve compiler error: no matching function for call to 'dmhFS::dmhFS()' in my case?
- Conda CMAKE CXX Compiler error while compiling Pytorch
Related Questions in UNIT-TESTING
- Google Truth.assertThat.contains does not behave similar to List.contains
- What's the best way to breakup a large test in pytest
- How to refer to the filepath of test data in test sourcecode?
- How to mock a dynamic endpoint in Apache Camel Spring Boot
- pytest mock failing when mocking function from imported package
- Jest configuration error while running test case in teamcity
- Resolve paths dynamically based on directory where test (or tested files) is located in Jest
- One-time implementation with Jest's mockResolvedValueOnce within test remains from one test to another
- Why can't I mock the decorator of the function?
- Gitlab pipeline stuck with nx cloud issue
- How to create an improperly closed gzip file using python?
- Nest.js service structure for API integration
- uiState not updating in Tests
- Unit Tests not Compiling or Being found without mod tests in main.rs. Is this a requirement or am I missing some configuration?
- Mocking Stream or Reader in Java Junit
Related Questions in QT4
- how to install Pyqt4 currently on raspberry pi 3 in python 2.7?
- GroupedDragging in Qt4
- Compiling Qt 4.5.0 without QT_NO_CAST_FROM_ASCII
- Compiling a Qt4 project with GitHub Actions on Windows
- When porting Qt4 to QT5, it prompts that #include<QX11Info> cannot be found
- Connecting a function to QHttp::requestFinished
- In Qt4.8, add a QToolButton to a QMenu, the auto connect not work
- Unable to retrieve the y-coordinate of the selected cell of a QTableWidget
- Python QThread don't start in a Gitlab Pipeline
- How to install qt4 on Jetson Nano with Ubuntu 20.04 for Hector Slam?
- nanopi neo air QtEmbedded-4.8.6-arm related
- How to investigate heap corruption on Windows?
- Convert custom type to QVariant
- How to remove default blue selection of QTreeWidget?
- How to convert a TIF file into a BMP file in C++98 using QT 4?
Related Questions in QTESTLIB
- qTest API call doesn't upload the attachment when I want to update the test case/test step
- How to use Single Sign On Credentials for QTest Rest APIs Access
- QSignalSpy error: No such signal when connecting a signal to a QSignalSpy
- Registering tests on QTest in CTest apart from executable like GTest realizations
- Select QMessageBox stored as local variable for QTest
- How to resolve warning "ignoring return value of function declared with 'warn_unused_result' attribute"
- How to run multiple tests in Qt without calling QTest::qExec multiple times?
- QTest::mouseClick RightButton can not work, can someone give some tips?
- Project Structure for unit testing (qtest) when using target_sources() command in sub directory
- Injecting a mock of a QTimer
- Using slots in QTest - Test case or slot?
- Qt creator and unwanted automatic prefix for a filename
- Robot framework: How to merge two xunit xml report files?
- How do I test the component given to the highlight property of a ListView?
- QTest covering paint method
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 # Hahtags
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.