Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Logging and Reporting in Python | Automating QA Tasks with Python
Python for QA Engineers

bookLogging 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.

1234567891011121314151617181920212223
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"}, ] 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}")
copy

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.

123456789101112131415161718192021
def 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))
copy

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.

question mark

Why is logging important in QA automation?

Select the correct answer

question mark

What is one way to improve the readability of test reports in Python?

Select the correct answer

question-icon

Fill in the blank to display the test's identifier in the log message using Python's f-string formatting.

: {status})
Test 1: PASS

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 6

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain how the report formatting works in the code?

What are some ways to further improve the readability of the logs?

How can I customize the summary to include additional test details?

bookLogging and Reporting in Python

Glissez pour afficher le menu

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.

1234567891011121314151617181920212223
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"}, ] 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}")
copy

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.

123456789101112131415161718192021
def 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))
copy

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.

question mark

Why is logging important in QA automation?

Select the correct answer

question mark

What is one way to improve the readability of test reports in Python?

Select the correct answer

question-icon

Fill in the blank to display the test's identifier in the log message using Python's f-string formatting.

: {status})
Test 1: PASS

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 6
some-alt