Keywords for this issue: unit testing, debugging, code style, static analysis, logging Difficulty: Beginner → Practical Advancement
📌 I. Basics of Unit Testing
1. What is the built-in unit testing framework in Python?
Explanation
- Built-in framework:
<span>unittest</span> - Common third-party frameworks:
<span>pytest</span>(more concise and efficient, frequently asked in interviews)
2. Write a simple unit test using <span>unittest</span>.
import unittest
def add(a, b):
return a + b
class TestAdd(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
unittest.main()
3. Test a function using <span>pytest</span>.
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
Run:
pytest -v
📌 II. Debugging Techniques
4. What are the disadvantages of using <span>print</span> for debugging?
Explanation
- Requires frequent code modifications
- Cannot view the state of complex objects in real-time
- Easily pollutes output, making debugging less elegant
5. Use <span>pdb</span> to start interactive debugging
import pdb
x = 10
pdb.set_trace() # The program will stop here
y = x + 5
print(y)
In the debugging interface, you can enter:
<span>n</span>(next): execute the next line<span>c</span>(continue): continue running<span>p variable_name</span>: print variable value
6. Use <span>logging</span> to record debugging information
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Program started")
logging.warning("This is a warning")
logging.error("An error occurred")
📌 III. Code Quality and Standards
7. What is PEP8?
ExplanationPEP8 is the official style guide for Python code, defining standards for indentation, naming, spacing, comments, etc. It is commonly asked in interviews, and tools are often used in actual projects to automatically check compliance.
8. Use <span>flake8</span> to check code style
pip install flake8
flake8 your_script.py
9. Use <span>black</span> to automatically format code
pip install black
black your_script.py
📌 IV. Static Analysis and Type Checking
10. Use <span>mypy</span> to check types
Example code:
def greet(name: str) -> str:
return f"Hello, {name}"
print(greet(123)) # Type error
Run:
mypy your_script.py
📌 V. Comprehensive Practice
Practical question: Implement and test a function to calculate the average
# avg.py
def average(nums):
if not nums:
raise ValueError("The list cannot be empty")
return sum(nums) / len(nums)
Corresponding test:
# test_avg.py
import pytest
from avg import average
def test_average_normal():
assert average([1, 2, 3]) == 2
def test_average_empty():
with pytest.raises(ValueError):
average([])
Run:
pytest -v
🎯 Conclusion
This issue focuses on testing, debugging, code style, and static analysis, which are key skills for junior Python engineers to improve development quality and avoid “brute-force debugging”.
Recommendations:
- ≥8 questions correct: Possess basic testing and debugging abilities, can independently ensure code quality in projects
- 5~7 questions correct: Need more practice, especially with pytest and logging
- <5 questions correct: It is recommended to start with simple projects and integrate testing and debugging into daily development
💬 How do you usually debug your code? Leave a comment to let me know, and see if there are ways to optimize!