I am trying to test a click based threading application with pytest. The application runs forever and waits for a keyboard event.
main.py
#!/usr/bin/python
import threading
import time
import typing
import logging
import click
def test_func(sample_var:str):
print("Sample is: " + sample_var)
@click.option("--sample", required=True, help="Sample String", default="sample", type=str)
@click.command()
def main(sample: str):
print("Starting App r")
interrupt = False
while not interrupt:
try:
print("Start threading ..")
my_thread = threading.Thread(
target=test_func,
kwargs=dict(sample_var=sample),
daemon=True)
my_thread.start()
my_thread.join(120)
if not interrupt:
print("Resting for 60 seconds")
time.sleep(60)
except(KeyboardInterrupt, SystemExit):
print("Received Keyboard Interrupt or system exisying, cleaning all the threads")
interrupt=True
if __name__ == "__main__":
main(auto_envvar_prefix="MYAPP")
The problem is that while testing I do not know how to send the Keyboard Interrupt Signal
main_test.py
from click.testing import CliRunner
from myapp.main import main
import pytest
import time
import click
def test_raimonitor_failing(cli_runner):
""" Tests the Startup off my app"""
runner = CliRunner()
params = ["--sample", "notsample"]
test = runner.invoke(cli = main, args = params)
expected_msg = "notsample\n"
print(test.stdout)
print(test.output)
assert 0
assert expected_msg in test.stdout
And the tests just hangs, and I don't know how to send the signal to stop it.
How can I test this system properly?