python
[Python] Get nodeid, module, function name using Pytest request built-in fixture
# to enable logging add to pytest.ini the following:
# [pytest]
#log_cli = 1
# log_level = INFO
# log_cli_level = INFO
# log_cli_format = %(asctime)s %(levelname)s %(message)s
# log_cli_date_format = %H:%M:%S
import pytest
import logging
# Example 1
def test_node_id(request):
node = request.node.nodeid
logging.info(f"\nThis is node: {node}")
module_name = request.module.__name__
logging.info(f"\nThis is module name: {module_name}")
function_name = request.function.__name__
logging.info(f"\nThis is function name: {function_name}")
fspath = request.fspath
logging.info(f"\nThis is fspath : {fspath}")
# Example 2
def test_func_node_id(request):
req = request
def foo(r):
node = r.node.nodeid
logging.info(f"
This is function node: {node}")
foo(req)
Output
================== Example 1 ==================
-------------------------------- live log call ---------------------------------
08:05:20 INFO
This is node: snippets/nodeid.py::test_node_id
08:05:20 INFO
This is module name: nodeid
08:05:20 INFO
This is function name: test_node_id
08:05:20 INFO
This is fspath : /Users/maksim/repos/p4-python-aerofiler/snippets/nodeid.py
================== Example 2 ==================
Process finished with exit code 0
-------------------------------- live log call ---------------------------------
20:49:43 INFO
This is function node: snippets/nodeid.py::test_func_node_id
PASSED
-------------------------------- live log call ---------------------------------
08:05:20 INFO
This is node: snippets/nodeid.py::test_node_id
08:05:20 INFO
This is module name: nodeid
08:05:20 INFO
This is function name: test_node_id
08:05:20 INFO
This is fspath : /Users/maksim/repos/p4-python-aerofiler/snippets/nodeid.py
================== Example 2 ==================
Process finished with exit code 0
-------------------------------- live log call ---------------------------------
20:49:43 INFO
This is function node: snippets/nodeid.py::test_func_node_id
PASSED
Was this helpful?
Similar Posts
- [Python] Using inspect.stack() to get module name, path, function name
- [Python] Pytest fixture example with cleanup and teardown
- Learn to Use Python's Built-In String Functions
- Column name as alias name SQLAlchemy
- How to generate a random number in python using random module
- Python Measure the execution time of small bits of Python code with the timeit module
- [Python] Two ways to import a module