Skip to content Skip to sidebar Skip to footer

How To Access The Gui Output?

I'm developing one test bench which runs multiple tests via python gui and prints the output as below. A Passed B Passed C Passed D Passed E Passed Button from gui should be chang

Solution 1:

If you're trying to get the text output of a program, you can't run that program using os.system. As the docs for that function say:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

If you follow those links, they'll show how to do what you want. But basically, it's something like this:

output = subprocess.check_output(["python", "nxptest.py", "my_testlist.txt"])

If you're using 2.6 or earlier, you won't have check_output; you can read the docs to see how to build it yourself on top of, e.g., communicate, or you can just install the subprocess32 backport from PyPI and use that.


From a comment:

This works but my only concern is there are lot of results for the tests which are printed before it actually prints A Passed B Passed etc.. Im looking for a way to get just this part of string and not the whole output.

That isn't possible. How could your program have any idea which part of the output is "this part of the string" and which part is "a lot of results … which are printed before"?

If you can edit the programs being tested in some way—e.g., make them print their "real" output to stdout, but their "extra" output to stderr, or provide a command-line argument that makes them skip all the extra stuff—that's great. But assuming you can't, there is no alternative but to filter the results.

But this doesn't look very hard. If each line of "real" output is either "X Passed" or "X Failed" and nothing else starts with "X " (where X is any uppercase ASCII letter), that's just:

test_results = {}
for line inoutput.splitlines():
    if line[0] instring.ascii_uppercase and line[1] == ' ':
        test_results[line[0]] = line[2:]

Now, at the end, you've got:

{'A': 'Passed', 'B': 'Passed', 'C': 'Passed', 'D': 'Passed', 'E': 'Passed'}

If you want to verify that all of A-E were covered and they all passed:

passed = (set(test_results) == set('ABCDE') andall(value == 'Passed'for value in test_results.values()))

Of course you could build something nicer that shows which ones were skipped or didn't pass or whatever. But honestly, if you want something more powerful, you should probably be using an existing unit testing framework instead of building one from scratch anyway.

Solution 2:

You can mask the printing output through a queue like so:

classFileQ(Queue.Queue):
    def__init__(self):
        Queue.Queue.__init__(self)

    defwrite(self,data):
        self.put(data)

classMyTestBench(QtGui.QWidget):
    def__init__(self):
        QtGui.QWidget.__init__(self)
        self.incoming = FileQ()
        self.setWindowTitle("Implementation")
        sys.stdout = self.incoming
        self.time = QtCore.QTimer()
        self.time.timeout.connect(self.refresh)
        self.time.start(10)
        self.t = QtGui.QTextEdit(self)
        self.t.setObjectName('TextOut')
        self.box = QtGui.QHBoxLayout(self)
        self.setLayout(self.box)
        self.box.addWidget(self.t)
        self.run_test_event()
        self.progressbar = QtGui.QProgressBar(self)
        self.progressbar.setObjectName('BarOut')
        self.box.addWidget(self.progressbar)

    defrun_test_event(self):
        thread.start_new_thread(self.run_the_test, ("Thread-1", 0.5))

    defrun_the_test(self,*args):
        for i inrange(10):
            print i
            time.sleep(1)

    defrefresh(self):        
        try:
            data = self.incoming.get_nowait()
        except:
            returnif data:
            self.t.insertPlainText(str(data))
            self.progressbar.setValue(int(data)*10)

Post a Comment for "How To Access The Gui Output?"