def test_subprocess_device_logging_in_file(self, caplog): """ Define the application wide queue handler for the logging, assign it to the device process. This test is about demonstrating actual usage in order to process the results in py.test. One of the confusing aspects here may be that in a bare bones application example, the log prints to the console still appear, on windows and linux. pytest does not see them however. Slurp them back in from the log file, which seems to work in executable, bare bones application, and pytest mode. """ assert applog.delete_log_file_if_exists() == True main_logger = applog.MainLogger() device = devices.LongPollingSimulateSpectra(main_logger.log_queue) """ NOTE: these sleeps are critical on windows. They do not seem to matter on linux though. They have to be in the order listed below or the pytest run will hang on cleanup. That is, all the tests will be run, assertions processed, and it just hangs at the end of the run. """ time.sleep(1.0) # make sure the process has enough time to emit device.close() main_logger.close() time.sleep(1.0) # required to let file creation happen log_text = applog.get_text_from_log() assert "SimulateSpectra setup" in log_text assert "SimulateSpectra setup" not in caplog.text() applog.explicit_log_close()
def test_subprocess_data_collect_is_logged_in_file(self, caplog): assert applog.delete_log_file_if_exists() == True main_logger = applog.MainLogger() device = devices.LongPollingSimulateSpectra(main_logger.log_queue) result = device.read() while result is None: time.sleep(0.2) print "Read: %s" % result result = device.read() assert len(result) == 1024 device.close() time.sleep(1.0) # make sure the process has enough time to emit main_logger.close() time.sleep(1.0) # required to let file creation happen log_text = applog.get_text_from_log() assert "Collected data in continuous" in log_text assert "Collected data in continuous" not in caplog.text() applog.explicit_log_close()
def test_log_capture_fixture_does_not_see_sub_process_entries( self, caplog): """ This test is about documenting the expected behavior. It took days of effort to determine that the logging is behaving as expected, but the pytest capture fixtures does not seem to be able to record those values. """ main_logger = applog.MainLogger() log_queue = main_logger.log_queue sub_proc = multiprocessing.Process(target=self.worker_process, args=(log_queue, )) sub_proc.start() time.sleep(1.0) # make sure the process has enough time to emit main_logger.close() time.sleep(0.5) # required to let file creation happen log_text = caplog.text() assert "Top level log configuration" in log_text assert "Sub process setup configuration" not in log_text assert "Sub process debug log info" not in log_text applog.explicit_log_close()
def test_log_file_has_sub_process_entries(self): """ This test documents the alternative: slurp the log results back in from the log file and then do the text matches. """ assert applog.delete_log_file_if_exists() == True main_logger = applog.MainLogger() log_queue = main_logger.log_queue sub_proc = multiprocessing.Process(target=self.worker_process, args=(log_queue, )) sub_proc.start() time.sleep(1.0) # make sure the process has enough time to emit main_logger.close() time.sleep(0.5) # required to let file creation happen log_text = applog.get_text_from_log() assert "Top level log configuration" in log_text assert "Sub process setup configuration" in log_text assert "Sub process debug log info" in log_text applog.explicit_log_close()
def test_log_file_is_created(self): assert applog.delete_log_file_if_exists() == True main_logger = applog.MainLogger() main_logger.close() time.sleep(0.5) # required to let file creation happen assert applog.log_file_created() == True applog.explicit_log_close()
def test_log_file_has_entries(self): assert applog.delete_log_file_if_exists() == True main_logger = applog.MainLogger() main_logger.close() time.sleep(0.5) # required to let file creation happen log_text = applog.get_text_from_log() assert "Top level log configuration" in log_text applog.explicit_log_close()
def test_view_logs_visible_to_caplog(self, caplog, qtbot): main_logger = applog.MainLogger() app_control = control.Controller(main_logger.log_queue) qtbot.wait(1000) app_control.close() time.sleep(1) main_logger.close() time.sleep(1) assert "Init of BasicWindow" in caplog.text() applog.explicit_log_close()
def run(self): """ This is the application code that is called by the main function. The architectural idea is to have as little code in main as possible and create the qapplication here so the testing code can function separately with pytest-qt. """ self.app = QtGui.QApplication([]) self.main_logger = applog.MainLogger() app_control = control.Controller(self.main_logger.log_queue) app_control.control_exit_signal.exit.connect(self.closeEvent) sys.exit(self.app.exec_())
def test_close_view_emits_control_signal(self, caplog, qtbot): """ Control script emits an event on a close condition to be processsed by the parent qt application, in this case qtbot. In the scripts file, it's the Qapplication. """ main_logger = applog.MainLogger() app_control = control.Controller(main_logger.log_queue) QtTest.QTest.qWaitForWindowShown(app_control.form) signal = app_control.control_exit_signal.exit with qtbot.wait_signal(signal, timeout=1): app_control.form.close() main_logger.close() time.sleep(1) assert "Control level close" in caplog.text() applog.explicit_log_close()
def basic_window(self, qtbot, request, hardware=None): """ Setup the controller the same way the scripts/Application does at every test. Ensure that the teardown is in place regardless of test result. """ main_logger = applog.MainLogger() app_control = control.Controller(main_logger.log_queue, hardware=hardware) qtbot.addWidget(app_control.form) def control_close(): app_control.close() main_logger.close() applog.explicit_log_close() request.addfinalizer(control_close) return app_control
def test_log_capture_fixture_can_read_top_level_log(self, caplog): main_logger = applog.MainLogger() main_logger.close() assert "Top level log configuration" in caplog.text() applog.explicit_log_close()