Created
August 19, 2018 21:59
-
-
Save enkore/14c53e9af79e9bbaf66478115605633f to your computer and use it in GitHub Desktop.
Coverage QThread Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from PyQt5.QtCore import QThread | |
class Worker(QThread): | |
def __init__(self): | |
QThread.__init__(self) | |
self.counter = 0 | |
def run(self): | |
import threading, sys | |
sys.settrace(threading._trace_hook) | |
while not self.isInterruptionRequested(): | |
self.calculate() | |
self.msleep(33) # 30Hz | |
def calculate(self): | |
self.counter += 1 | |
def main(): | |
w = Worker() | |
w.start() | |
assert w.isRunning() | |
time.sleep(2) | |
w.requestInterruption() | |
w.wait() | |
assert w.isFinished() | |
assert w.counter > 0 | |
print("Counter: ", w.counter) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change main to "test_main" and then run under pytest.... notice you get coverage for the
counter
? but you won't get coverage forrun
.