সংস্করণ ৩.৩, pytestলাইভ লগিং সমর্থন করে, যার অর্থ পরীক্ষার মধ্যে নির্ধারিত সমস্ত লগ রেকর্ডগুলি অবিলম্বে টার্মিনালে মুদ্রিত হবে। বৈশিষ্ট্যটি লাইভ লগস বিভাগের অধীনে নথিভুক্ত করা হয়েছে । লাইভ লগিং ডিফল্টরূপে অক্ষম করা হয়; এটি সক্রিয় করতে, সেট log_cli = 1মধ্যে pyproject.toml1 বা pytest.ini2 কনফিগ। লাইভ লগিং টার্মিনাল এবং ফাইলে নির্গতকে সমর্থন করে; সম্পর্কিত বিকল্প রেকর্ড কাস্টমাইজ করার অনুমতি দেয়:
টার্মিনাল:
log_cli_level
log_cli_format
log_cli_date_format
ফাইল:
log_file
log_file_level
log_file_format
log_file_date_format
দ্রষ্টব্য : log_cliপতাকাটি কমান্ড লাইন থেকে প্রেরণ করা যাবে না এবং অবশ্যই সেট আপ করতে হবে pytest.ini। অন্যান্য সমস্ত বিকল্প উভয়ই কমান্ড লাইন থেকে পাস বা কনফিগার ফাইলে সেট করা যেতে পারে। হিসাবে দ্বারা নির্দিষ্ট কেভিন Barre মধ্যে এই মন্তব্যটি , কম্যান্ড লাইন থেকে স্টার অপশন অগ্রাহ্য মাধ্যমে করা যাবে -o/--overrideবিকল্প। প্রকাশক সুতরাং পরিবর্তে log_cliমধ্যে pytest.ini, আপনি কেবল কল করতে পারেন:
$ pytest -o log_cli=true ...
উদাহরণ
প্রদর্শনের জন্য ব্যবহৃত সাধারণ পরীক্ষা ফাইল:
import logging
LOGGER = logging.getLogger(__name__)
def test_eggs():
LOGGER.info('eggs info')
LOGGER.warning('eggs warning')
LOGGER.error('eggs error')
LOGGER.critical('eggs critical')
assert True
আপনি দেখতে পাচ্ছেন যে অতিরিক্ত কোনও কনফিগারেশন প্রয়োজন নেই; কমান্ড লাইন থেকে pytestনির্দিষ্ট pytest.iniবা পাস করা বিকল্পগুলির উপর ভিত্তি করে স্বয়ংক্রিয়ভাবে লগার সেটআপ করবে ।
টার্মিনাল, INFOস্তর, অভিনব আউটপুটে লাইভ লগিং
এতে কনফিগারেশন pyproject.toml:
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "INFO"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
উত্তরাধিকারসূত্রে অভিন্ন কনফিগারেশন pytest.ini:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
পরীক্ষা চলছে:
$ pytest test_spam.py
=============================== test session starts ================================
platform darwin -- Python 3.6.4, pytest-3.7.0, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-4673373, inifile: pytest.ini
collected 1 item
test_spam.py::test_eggs
---------------------------------- live log call -----------------------------------
2018-08-01 14:33:20 [ INFO] eggs info (test_spam.py:7)
2018-08-01 14:33:20 [ WARNING] eggs warning (test_spam.py:8)
2018-08-01 14:33:20 [ ERROR] eggs error (test_spam.py:9)
2018-08-01 14:33:20 [CRITICAL] eggs critical (test_spam.py:10)
PASSED [100%]
============================= 1 passed in 0.01 seconds =============================
টার্মিনাল এবং ফাইলে লাইভ লগিং, কেবলমাত্র বার্তা এবং CRITICALটার্মিনালের স্তর, pytest.logফাইলে অভিনব আউটপুট
এতে কনফিগারেশন pyproject.toml:
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "CRITICAL"
log_cli_format = "%(message)s"
log_file = "pytest.log"
log_file_level = "DEBUG"
log_file_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_file_date_format = "%Y-%m-%d %H:%M:%S"
উত্তরাধিকারসূত্রে অভিন্ন কনফিগারেশন pytest.ini:
[pytest]
log_cli = 1
log_cli_level = CRITICAL
log_cli_format = %(message)s
log_file = pytest.log
log_file_level = DEBUG
log_file_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format=%Y-%m-%d %H:%M:%S
পরীক্ষা রান:
$ pytest test_spam.py
=============================== test session starts ================================
platform darwin -- Python 3.6.4, pytest-3.7.0, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-4673373, inifile: pytest.ini
collected 1 item
test_spam.py::test_eggs
---------------------------------- live log call -----------------------------------
eggs critical
PASSED [100%]
============================= 1 passed in 0.01 seconds =============================
$ cat pytest.log
2018-08-01 14:38:09 [ INFO] eggs info (test_spam.py:7)
2018-08-01 14:38:09 [ WARNING] eggs warning (test_spam.py:8)
2018-08-01 14:38:09 [ ERROR] eggs error (test_spam.py:9)
2018-08-01 14:38:09 [CRITICAL] eggs critical (test_spam.py:10)
pyproject.tomlসংস্করণ 6.0 থেকে 1 সমর্থিত এবং সেরা বিকল্প আইএমও। চশমার জন্য পিইপি 518 দেখুন ।
2 আপনি সেটাও কন্ফিগার করতে পারেন যদিও pytestমধ্যে setup.cfgঅধীনে [tool:pytest]অধ্যায়, যা করতে প্রলুব্ধ না হতে যে আপনি যখন কাস্টম লাইভ লগিং বিন্যাস প্রদান করতে চান। অন্যান্য সরঞ্জামগুলি পঠন স্ট্রিং প্রবৃদ্ধির setup.cfgমতো স্টাফকে চিকিত্সা করতে পারে %(message)sএবং ব্যর্থ হতে পারে। সর্বোত্তম পছন্দটি pyproject.tomlযেভাবেই ব্যবহার করা হচ্ছে তবে আপনি যদি উত্তরাধিকারের ইন-স্টাইল বিন্যাসটি ব্যবহার করতে বাধ্য হন তবে pytest.iniত্রুটিগুলি এড়াতে আটকে থাকুন ।