Can I unittest Qt elements?
-
Is there a way or does Qt offer features to unittest some elements?
An example would be a simple dialog with a textedit field and an OK and Cancel button. A config variable would change its value based on the input the user makes in that dialog and pushing OK.
Can I test that isolated without having an X-server running and with simulating user input?
-
Hi,
Yes and yes.
You can use pytest-qt to write your tests and use the offscreen QPA to run your tests without any screen.
-
@SGaist said in Can I unittest Qt elements?:
You can use pytest-qt to write your tests and use the offscreen QPA to run your tests without any screen.
Thanks for the hints. I found the pytest-qt docu. I wouldn't use pytest if I doesn't need to. I hope there is a solution for usual Python unittests. If not I think Qt would be a reason to dive deeper into pytest (which I already know but don't like to use).
What is QPA?
Do you have a tutorial or a public repo where I can see that in action?
The Qt docu is not the best when it comes to real examples and Python code. I just checked the API-reference of QTest and QSpySignal. But there is C code in the examples. I don't know how to combine this with
unittest.TestCase
. -
From memory you can use classic Python unittests with pytest. Frankly, pytest-qt makes things really simple and easy for Qt related unit testing that you really should use it.
Here you have the QPA (Qt Platform Abstraction) documentation.
As for the examples, use the pytest-qt documentation. The examples from the Qt for Python documentation are translated automatically and this can leave some less than ideal result but this is being worked on.
-
@SGaist said in Can I unittest Qt elements?:
From memory you can use classic Python unittests with pytest.
But I don't want to use pytest.
Frankly, pytest-qt makes things really simple and easy for Qt related unit testing that you really should use it.
"simple" is the problem. PyTest hide to much and has to much backroom automatics. I lose control when using PyTest.
Here you have the QPA (Qt Platform Abstraction) documentation.
Doesn't help. No python example code.
As for the examples, use the pytest-qt documentation.
Doesn't show me tests derived from unittest.TestCase.
The examples from the Qt for Python documentation are translated automatically
Yes I am aware of that problem.
-
@buhtz said in Can I unittest Qt elements?:
@SGaist said in Can I unittest Qt elements?:
From memory you can use classic Python unittests with pytest.
But I don't want to use pytest.
Up to you
Frankly, pytest-qt makes things really simple and easy for Qt related unit testing that you really should use it.
"simple" is the problem. PyTest hide to much and has to much backroom automatics. I lose control when using PyTest.
What exactly are you losing ?
Here you have the QPA (Qt Platform Abstraction) documentation.
Doesn't help. No python example code.
That was not the goal, it is just information about QPA. In your case, you need to set the QT_QPA_PLATFORM environment variable before calling the unit tests.
As for the examples, use the pytest-qt documentation.
Doesn't show me tests derived from unittest.TestCase.
This stack overflow answer might give you a start.
The examples from the Qt for Python documentation are translated automatically
Yes I am aware of that problem.