Skip to content

Instantly share code, notes, and snippets.

@enkore
Created August 19, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enkore/14c53e9af79e9bbaf66478115605633f to your computer and use it in GitHub Desktop.
Save enkore/14c53e9af79e9bbaf66478115605633f to your computer and use it in GitHub Desktop.
Coverage QThread Test
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()
@earonesty
Copy link

Change main to "test_main" and then run under pytest.... notice you get coverage for the counter? but you won't get coverage for run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment