Logging and Reporting in Python
Understanding the importance of logging in QA is crucial for building reliable automation. Logging helps you track what happened during test execution, making it easier to identify issues, reproduce bugs, and communicate results to your team. In Python, even the simplest form of logging can be achieved using print statements, which let you display the outcome of each test case directly in the console. By combining print statements with Python's string formatting capabilities, you can produce clear, readable logs and basic reports that summarize your test results. This approach is especially useful when you are starting out or working on lightweight test automation projects.
1234567891011121314151617181920212223test_cases = [ {"id": 1, "name": "Login Test", "result": "PASS"}, {"id": 2, "name": "Logout Test", "result": "FAIL"}, {"id": 3, "name": "Profile Update Test", "result": "PASS"}, {"id": 4, "name": "Password Reset Test", "result": "PASS"}, ] passed = 0 failed = 0 print("Test Execution Log:") print("-------------------") for case in test_cases: status = case["result"] if status == "PASS": passed += 1 else: failed += 1 print(f"Test {case['id']}: {case['name']} - {status}") print("\nSummary Report:") print("--------------") print(f"Total: {len(test_cases)} | Passed: {passed} | Failed: {failed}")
Formatting your log messages is key to making your output clear and actionable. Use consistent patterns such as including the test ID, name, and status in every log entry. This helps you quickly scan the results and spot failures or trends. Aggregating results at the end of execution, such as counting the number of passed and failed tests, gives you a high-level view of your test run and helps stakeholders understand the overall quality status. By grouping and summarizing outcomes, you ensure your report is both concise and informative.
123456789101112131415161718192021def generate_report(test_cases): report_lines = [] passed = sum(1 for c in test_cases if c["result"] == "PASS") failed = sum(1 for c in test_cases if c["result"] == "FAIL") report_lines.append("Test Execution Summary") report_lines.append("======================") for case in test_cases: line = f"[{case['result']}] {case['id']:02d} - {case['name']}" report_lines.append(line) report_lines.append("----------------------") report_lines.append(f"Total: {len(test_cases)}, Passed: {passed}, Failed: {failed}") return "\n".join(report_lines) test_cases = [ {"id": 1, "name": "Login Test", "result": "PASS"}, {"id": 2, "name": "Logout Test", "result": "FAIL"}, {"id": 3, "name": "Profile Update Test", "result": "PASS"}, {"id": 4, "name": "Password Reset Test", "result": "PASS"}, ] print(generate_report(test_cases))
1. Why is logging important in QA automation?
2. What is one way to improve the readability of test reports in Python?
3. Fill in the blank to display the test's identifier in the log message using Python's f-string formatting.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 4.76
Logging and Reporting in Python
Свайпніть щоб показати меню
Understanding the importance of logging in QA is crucial for building reliable automation. Logging helps you track what happened during test execution, making it easier to identify issues, reproduce bugs, and communicate results to your team. In Python, even the simplest form of logging can be achieved using print statements, which let you display the outcome of each test case directly in the console. By combining print statements with Python's string formatting capabilities, you can produce clear, readable logs and basic reports that summarize your test results. This approach is especially useful when you are starting out or working on lightweight test automation projects.
1234567891011121314151617181920212223test_cases = [ {"id": 1, "name": "Login Test", "result": "PASS"}, {"id": 2, "name": "Logout Test", "result": "FAIL"}, {"id": 3, "name": "Profile Update Test", "result": "PASS"}, {"id": 4, "name": "Password Reset Test", "result": "PASS"}, ] passed = 0 failed = 0 print("Test Execution Log:") print("-------------------") for case in test_cases: status = case["result"] if status == "PASS": passed += 1 else: failed += 1 print(f"Test {case['id']}: {case['name']} - {status}") print("\nSummary Report:") print("--------------") print(f"Total: {len(test_cases)} | Passed: {passed} | Failed: {failed}")
Formatting your log messages is key to making your output clear and actionable. Use consistent patterns such as including the test ID, name, and status in every log entry. This helps you quickly scan the results and spot failures or trends. Aggregating results at the end of execution, such as counting the number of passed and failed tests, gives you a high-level view of your test run and helps stakeholders understand the overall quality status. By grouping and summarizing outcomes, you ensure your report is both concise and informative.
123456789101112131415161718192021def generate_report(test_cases): report_lines = [] passed = sum(1 for c in test_cases if c["result"] == "PASS") failed = sum(1 for c in test_cases if c["result"] == "FAIL") report_lines.append("Test Execution Summary") report_lines.append("======================") for case in test_cases: line = f"[{case['result']}] {case['id']:02d} - {case['name']}" report_lines.append(line) report_lines.append("----------------------") report_lines.append(f"Total: {len(test_cases)}, Passed: {passed}, Failed: {failed}") return "\n".join(report_lines) test_cases = [ {"id": 1, "name": "Login Test", "result": "PASS"}, {"id": 2, "name": "Logout Test", "result": "FAIL"}, {"id": 3, "name": "Profile Update Test", "result": "PASS"}, {"id": 4, "name": "Password Reset Test", "result": "PASS"}, ] print(generate_report(test_cases))
1. Why is logging important in QA automation?
2. What is one way to improve the readability of test reports in Python?
3. Fill in the blank to display the test's identifier in the log message using Python's f-string formatting.
Дякуємо за ваш відгук!