QtTest: How to load a json file in a TestCase

Hi,

how can i load a json file in a QtTest TestCase and use it as a input paramter for one
of my functions to test?

    import QtQuick 2.0
    import QtTest 1.2

    TestCase {
        name: "Function Tests"
       function test_functions_calculateSomething() {
        compare("-", Functions.calc(theJsonInput))
    }
    }

I do not want to create a string from the JSON input - because the json input data can be quite long and is not really maintainable this way.

I guess that is the same as any QML; XMLHttpRequest (yes even for local files), or your own c++ class that provides it.

Do you have an example for the XMLHttpRequest option? I do not want to write a c++ class that provides the json only for a test - the JavaScript tests shall be JS only, to reduce build complexity.

Well, this is for a list model, but you can just do whatever you wanted instead when you get the JSON.

Thanks for the reply - the proposed solution works for me like this - make sure to use false as last argument in the open method - to make a synchronous call (in my case):

        var xhr = new XMLHttpRequest;
        xhr.open("GET", "testdata/bla.json", false); // false - load synchronously
        xhr.onreadystatechange = function () {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                 var data = xhr.responseText;
            }
        }
        xhr.send()
2 Likes