mirror of
https://github.com/SeedSigner/seedsigner.git
synced 2026-10-05 15:08:25 +00:00
Fixes to restore log access in pytest
This commit is contained in:
+1
-1
@@ -58,7 +58,7 @@ branch = true
|
|||||||
|
|
||||||
[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
testpaths = ["tests"]
|
testpaths = ["tests"]
|
||||||
addopts = "-p no:logging"
|
log_level = "DEBUG"
|
||||||
|
|
||||||
[tool.setuptools]
|
[tool.setuptools]
|
||||||
include-package-data = true
|
include-package-data = true
|
||||||
|
|||||||
@@ -31,6 +31,20 @@ Run a specific test:
|
|||||||
pytest tests/test_this_file.py::test_this_specific_test
|
pytest tests/test_this_file.py::test_this_specific_test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Force pytest to show logging output:
|
||||||
|
```bash
|
||||||
|
pytest tests/test_this_file.py::test_this_specific_test -o log_cli=1
|
||||||
|
|
||||||
|
# or (same result)
|
||||||
|
|
||||||
|
pytest tests/test_this_file.py::test_this_specific_test --log-cli-level=DEBUG
|
||||||
|
```
|
||||||
|
|
||||||
|
Annoying complications:
|
||||||
|
* If you want to see `print()` statements that are in a test file, add `-s`
|
||||||
|
* Better idea: use a proper logger in the test file and use one of the above options to display logs
|
||||||
|
|
||||||
|
|
||||||
### Test Coverage
|
### Test Coverage
|
||||||
Run tests and generate test coverage
|
Run tests and generate test coverage
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ from seedsigner.hardware.microsd import MicroSD
|
|||||||
from seedsigner.models.settings import Settings
|
from seedsigner.models.settings import Settings
|
||||||
from seedsigner.views.view import Destination, MainMenuView, View
|
from seedsigner.views.view import Destination, MainMenuView, View
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -93,6 +95,28 @@ class BaseTest:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class TestBaseTest(BaseTest):
|
||||||
|
def test_howto_log_from_test(self):
|
||||||
|
"""
|
||||||
|
Not actually a test, just a demonstration of how to use/access logs while
|
||||||
|
testing.
|
||||||
|
|
||||||
|
Enable log visibility by running with:
|
||||||
|
--log-cli-level=NOTSET (or the level of your choice)
|
||||||
|
-o log_cli=1
|
||||||
|
|
||||||
|
Enable print() visibility by running with:
|
||||||
|
-s or --capture=no
|
||||||
|
"""
|
||||||
|
print("This is a test print message")
|
||||||
|
logger.info("This is a test log message")
|
||||||
|
logger.debug("This is a test debug message")
|
||||||
|
logger.warning("This is a test warning message")
|
||||||
|
logger.error("This is a test error message")
|
||||||
|
logger.critical("This is a test critical message")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class FlowStep:
|
class FlowStep:
|
||||||
"""
|
"""
|
||||||
|
|||||||
+35
-25
@@ -1,40 +1,50 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import pytest
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import patch, call
|
from unittest.mock import patch, call
|
||||||
|
|
||||||
import pytest
|
# Must import from base.py before any other SeedSigner dependency to mock out certain imports
|
||||||
|
from base import BaseTest
|
||||||
|
|
||||||
sys.path.insert(0,'src')
|
sys.path.insert(0,'src')
|
||||||
from main import main
|
from main import main
|
||||||
|
|
||||||
|
|
||||||
@patch("main.Controller")
|
class TestMain(BaseTest):
|
||||||
def test_main__argparse__default(patched_controller):
|
"""
|
||||||
main([])
|
The `main.Controller` has to be patched / mocked out otherwise the virtual SeedSigner
|
||||||
assert logging.root.level == logging.INFO
|
will just keep running and cause the test to hang.
|
||||||
assert logging.getLogger().getEffectiveLevel() == logging.INFO
|
"""
|
||||||
patched_controller.assert_has_calls(
|
@patch("main.Controller")
|
||||||
[call.get_instance(), call.get_instance().start()]
|
def test_main__argparse__default(self, patched_controller):
|
||||||
)
|
main([])
|
||||||
|
assert logging.root.level == logging.INFO
|
||||||
|
assert logging.getLogger().getEffectiveLevel() == logging.INFO
|
||||||
|
patched_controller.assert_has_calls(
|
||||||
|
[call.get_instance(), call.get_instance().start()]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@patch("main.Controller")
|
@patch("main.Controller")
|
||||||
def test_main__argparse__enable_debug_logging(patched_controller):
|
def test_main__argparse__enable_debug_logging(self, patched_controller):
|
||||||
main(["--loglevel", "DEBUG"])
|
main(["--loglevel", "DEBUG"])
|
||||||
assert logging.root.level == logging.DEBUG
|
assert logging.root.level == logging.DEBUG
|
||||||
assert logging.getLogger().getEffectiveLevel() == logging.DEBUG
|
assert logging.getLogger().getEffectiveLevel() == logging.DEBUG
|
||||||
patched_controller.assert_has_calls(
|
patched_controller.assert_has_calls(
|
||||||
[call.get_instance(), call.get_instance().start()]
|
[call.get_instance(), call.get_instance().start()]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_main__argparse__invalid_arg():
|
def test_main__argparse__invalid_arg(self):
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
main(["--invalid"])
|
main(["--invalid"])
|
||||||
|
|
||||||
|
|
||||||
@patch("main.Controller")
|
@patch("main.Controller")
|
||||||
def test_main__logging__writes_to_stderr(patched_controller, capsys):
|
def test_main__logging__writes_to_stderr(self, patched_controller, capsys):
|
||||||
main([])
|
main([])
|
||||||
_, err = capsys.readouterr()
|
_, err = capsys.readouterr()
|
||||||
assert "Starting SeedSigner" in err and "INFO" in err
|
assert "Starting SeedSigner" in err and "INFO" in err
|
||||||
|
patched_controller.assert_has_calls(
|
||||||
|
[call.get_instance(), call.get_instance().start()]
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user