如何设置日志文件不可更改的路径

问题描述 投票:0回答:1

我有不同嵌套级别的测试文件。 例如:

/项目/测试/流程/test_flow_1.py

/项目/测试/订单/报告/test_reports.py

我在

pytest.ini
中的日志路径设置是
log_file = logs/tests.log

但是 pytest 在每个测试目录中创建目录“logs”。

我也尝试将其设置在

conftest.py
文件中,但日志文件并未创建:

def logs_path(request):
    path = request.config.rootdir
    logging.basicConfig(filename=os.path.join(path, 'tests.log'))

如何设置在根项目目录中只创建一次?

python automated-tests pytest
1个回答
0
投票

我尝试使用此目录结构复制您的测试:

├── flow
│   └── test_flow.py
├── logs
│   └── my.log
├── orders
│   └── reports
│       └── test_reports.py
└── pytest.ini

pytest.ini:

[pytest]
log_cli = 1
log_cli_level = INFO
log_file = logs/my.log
log_file_level = DEBUG

pyest.ini
所在的级别,我调用 pytest:

$ pytest
...
collected 2 items

flow/test_flow.py::test_flow
------------------------------------ live log call -------------------------------------
INFO     root:test_flow.py:3 in test flow
PASSED                                                                           [ 50%]
orders/reports/test_reports.py::test_reports
------------------------------------ live log call -------------------------------------
INFO     root:test_reports.py:5 in test reports
PASSED                                                                           [100%]

================================== 2 passed in 0.01s ===================================

通过这些设置,我只能看到一个

logs/my.log
文件,其内容是:

INFO     root:test_flow.py:3 in test flow
INFO     root:test_reports.py:5 in test reports

请检查您的设置、调用测试的方式以及调用的位置。

© www.soinside.com 2019 - 2024. All rights reserved.