You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_console.py 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from autogen.io import IOConsole
  4. class TestConsoleIO:
  5. def setup_method(self) -> None:
  6. self.console_io = IOConsole()
  7. @patch("builtins.print")
  8. def test_print(self, mock_print: MagicMock) -> None:
  9. # calling the print method should call the mock of the builtin print function
  10. self.console_io.print("Hello, World!", flush=True)
  11. mock_print.assert_called_once_with("Hello, World!", end="\n", sep=" ", flush=True)
  12. @patch("builtins.input")
  13. def test_input(self, mock_input: MagicMock) -> None:
  14. # calling the input method should call the mock of the builtin input function
  15. mock_input.return_value = "Hello, World!"
  16. actual = self.console_io.input("Hi!")
  17. assert actual == "Hello, World!"
  18. mock_input.assert_called_once_with("Hi!")
  19. @pytest.mark.parametrize("prompt", ["", "Password: ", "Enter you password:"])
  20. def test_input_password(self, monkeypatch: pytest.MonkeyPatch, prompt: str) -> None:
  21. mock_getpass = MagicMock()
  22. mock_getpass.return_value = "123456"
  23. monkeypatch.setattr("getpass.getpass", mock_getpass)
  24. actual = self.console_io.input(prompt, password=True)
  25. assert actual == "123456"
  26. if prompt == "":
  27. mock_getpass.assert_called_once_with("Password: ")
  28. else:
  29. mock_getpass.assert_called_once_with(prompt)