Unit testing - zamaniamin/python GitHub Wiki
1. What is unit testing, and why is it important?
Unit testing is a process of testing individual units of code in isolation to ensure that they function correctly. It is important because it helps identify bugs and issues early in the development process, which can save time and resources in the long run.
2. How do you write a unit test in Python?
In Python, you can write a unit test using the built-in unittest module. You define a class that inherits from unittest.TestCase and then write test methods that assert the expected behavior of the code being tested.
3. What is test-driven development (TDD), and how does it work?
Test-driven development is a development approach where tests are written before the code is written. The idea is that writing tests first helps to clarify the requirements and expectations for the code, and can lead to better code quality and more efficient development.
4. What is a test fixture, and how is it used in testing?
A test fixture is a set of initial conditions that are set up before running a test. It can include things like creating test data, initializing objects, or starting a server.
5. What is mocking, and when might you use it in testing?
Mocking is a technique for creating fake objects that simulate the behavior of real objects in a controlled way. It is often used in testing to replace complex or external dependencies with simpler, controlled objects.
6. What is a test runner, and how do you use it?
A test runner is a tool that executes tests and reports on the results. In Python, the built-in unittest module provides a test runner.
7. What is code coverage, and why is it important in testing?
Code coverage is a metric that measures how much of your code is executed during tests. It is important in testing because it can help identify areas of code that may not be properly tested and may contain bugs.
8. How do you write integration tests in Python?
Integration tests in Python are tests that verify the interactions between different components of a system, such as different modules or services.
9. How do you write functional tests in Python?
Functional tests in Python are tests that verify that a system or component behaves correctly from a user or system perspective.
10. What is behavior-driven development (BDD), and how does it differ from TDD?
Behavior-driven development is a development approach that emphasizes defining the behavior of a system in terms of user stories and acceptance criteria, and writing tests based on those stories and criteria. It differs from TDD in that it focuses on the behavior of the system rather than individual units of code.
11. What is acceptance testing, and how do you perform it in Python?
Acceptance testing is a type of testing that verifies that a system or component meets a set of predefined requirements or acceptance criteria.
12. What is load testing, and how do you perform it in Python?
Load testing is a type of testing that simulates a high volume of traffic or requests to test how a system or component performs under heavy load.
13. What is stress testing, and how do you perform it in Python?
Stress testing is a type of testing that simulates extreme conditions, such as high traffic or limited resources, to test how a system or component performs under stress.
14. How do you test asynchronous code in Python?
Asynchronous code in Python can be tested using the asyncio module, which provides tools for running and testing asynchronous code.
15. How do you test code that interacts with a database?
Code that interacts with a database can be tested using tools like mock databases or test databases that are set up specifically for testing.
16. What is regression testing, and how do you perform it in Python?
Regression testing is a type of testing that verifies that changes to a system or component do not introduce new bugs or issues.
17. How do you write testable code in Python?
To write testable code in Python, it is important to write code that is modular, with clear inputs and outputs, and that avoids dependencies on external resources or services.
18. How do you measure the performance of your tests in Python?
Performance of tests in Python can be measured using tools like pytest-benchmark, which provides tools for measuring and comparing test performance.
19. How do you deal with flaky tests in Python?
Flaky tests are tests that produce inconsistent results or false positives/negatives. They can be addressed by identifying the root cause of the flakiness and making changes to the test or code to prevent it from happening.
20. How do you ensure that your tests are maintainable over time?
To ensure that tests are maintainable over time, it is important to write tests that are easy to understand, with clear documentation and meaningful test names. It is also important to regularly review and update tests as the code changes.