def test_integration_file(self, tmpdir, monkeypatch, capsys): """Tests that a blackbird script was loaded and samples were written to the specified output file using the run_blackbird_script function.""" filepath = tmpdir.join("test_script.xbb") with open(filepath, "w") as f: f.write(TEST_SCRIPT) mocked_args = MockArgs() mocked_args.input = filepath out_filepath = tmpdir.join("test_script.xbb") mocked_args.output = out_filepath with monkeypatch.context() as m: m.setattr(cli, "RemoteEngine", MockRemoteEngineIntegration) cli.run_blackbird_script(mocked_args) with open(filepath, "r") as f: results_from_file = f.read() out, _ = capsys.readouterr() assert out == "Executing program on remote hardware...\n" assert results_from_file == str(Result(test_samples).samples)
def test_exit_if_file_not_found(self, monkeypatch, capsys): """Tests that if the input script file was not found then a system exit occurs along with a message being outputted.""" mocked_program = MockProgram() mocked_args = MockArgs() def mock_load(arg): raise FileNotFoundError with monkeypatch.context() as m: m.setattr(cli, "load", mock_load) with pytest.raises(SystemExit): cli.run_blackbird_script(mocked_args) out, _ = capsys.readouterr() assert "blackbird script was not found" in out
def test_result_is_none(self, monkeypatch, capsys): """Tests that the write_script_results function is not called if the results from the run method of the engine returned a None.""" mocked_program = MockProgram() mocked_args = MockArgs() mocked_write_script_results = MockWriteScriptResults() with monkeypatch.context() as m: m.setattr(cli, "load", lambda arg: mocked_program) m.setattr(cli, "RemoteEngine", MockRemoteEngine) m.setattr(cli, "write_script_results", mocked_write_script_results.write_script_results) with pytest.raises(SystemExit): cli.run_blackbird_script(mocked_args) out, _ = capsys.readouterr() assert "Executing program on remote hardware..." in out # Check that the write_script_results function was not called assert not mocked_write_script_results.called
def test_integration_std_out(self, tmpdir, monkeypatch, capsys): """Tests that a blackbird script was loaded and samples were written to the standard output using the run_blackbird_script function.""" filepath = tmpdir.join("test_script.xbb") with open(filepath, "w") as f: f.write(TEST_SCRIPT) mocked_args = MockArgs() mocked_args.input = filepath with monkeypatch.context() as m: m.setattr(cli, "RemoteEngine", MockRemoteEngineIntegration) cli.run_blackbird_script(mocked_args) out, err = capsys.readouterr() execution_message = "Executing program on remote hardware...\n" outputs = execution_message + str(Result(test_samples).samples) assert outputs == out