From f24fe1180960f45c5c4280c6b5090e195a9c420d Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 13 Sep 2024 13:21:25 -0400 Subject: [PATCH] Fixed unit test for Python 3.8+ systems. --- tests/test_cmd2.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 2130cbf1..1ddbdeb4 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -75,16 +75,20 @@ def test_version(base_app): assert cmd2.__version__ -@pytest.mark.skipif(sys.version_info >= (3, 8), reason="failing in CI systems for Python 3.8 and 3.9") def test_not_in_main_thread(base_app, capsys): import threading - cli_thread = threading.Thread(name='cli_thread', target=base_app.cmdloop) + # Mock threading.main_thread() to return our fake thread + saved_main_thread = threading.main_thread + fake_main = threading.Thread() + threading.main_thread = mock.MagicMock(name='main_thread', return_value=fake_main) - cli_thread.start() - cli_thread.join() - out, err = capsys.readouterr() - assert "cmdloop must be run in the main thread" in err + with pytest.raises(RuntimeError) as excinfo: + base_app.cmdloop() + + # Restore threading.main_thread() + threading.main_thread = saved_main_thread + assert "cmdloop must be run in the main thread" in str(excinfo.value) def test_empty_statement(base_app):