KEMBAR78
Presentation on Unit Test with Python and Pytest | PDF
Introduction to Unit
Testing
Presented by
Md. Abdullah Al Arif
One Direction Companies Limited
Enhancing Code Reliability One Test at a Time
1
Table of contents
01 What is unit test
02 Why Unit Testing is Important ?
03 Key Characteristics of Unit Tests
04 Best Practices for Writing Unit Tests
05 Common Unit Testing Frameworks
06 Structure of a Unit Test
07 Best Practices for Writing Unit Tests
08 Mocking and Stubbing
09 Challenges in Unit Testing
10 Unit Testing vs Other Types of Testing
11 Tools for Code Coverage
12 Demo
13 Key Takeaways
14 Q&A
2
What is Unit Test
Testing individual units or components of a software
application to ensure they work as expected.
Focus: Smallest testable parts, like functions or methods.
Goal: Identify and fix bugs early in development.
3
Why Unit Testing is Important ?
● Ensures code reliability and correctness.
● Helps detect bugs early in the development cycle.
● Simplifies debugging by isolating the problematic unit.
● Improves code quality and maintainability.
4
Key Characteristics of Unit Tests
Isolated: Tests a single "unit" without external dependencies.
Automated: Run tests programmatically.
Repeatable: Consistent results every time.
Fast: Quick execution to maintain workflow efficiency.
5
Common Unit Testing Frameworks
For Python:
● unittest (built-in).
● pytest (popular and user-friendly).
For Java:
● JUnit.
For JavaScript:
● Jest, Mocha.
Others:
● NUnit (C#), PHPUnit (PHP), etc.
6
Structure of a unit test
● Setup: Prepare the environment and inputs.
● Execution: Call the unit with the prepared inputs.
● Assertion: Check if the output matches expected results.
● Teardown: Clean up (optional).
7
Best Practices for Writing Unit Tests
● Write tests for edge cases and common use cases.
● Use descriptive names for test methods.
● Keep tests simple and focused on one functionality.
● Mock external dependencies (e.g., databases, APIs).
● Continuously run tests during development.
8
Mocking and Stubbing
Mocking: Replacing a real object with a "fake" version to isolate the unit under
test.
Stubbing: Providing pre-defined responses from dependencies.
Example: Testing a function that calls an external API by mocking the API
response.
9
Challenges in Unit Testing
● Testing private or complex functions.
● Dependency on legacy code with poor testability.
● Writing tests for highly coupled or poorly designed
code.
● Balancing test coverage vs. development time.
10
Unit Testing vs Other Types of
Testing
Aspect Unit Testing Integration Testing System Testing
Scope Smallest unit Modules/components Whole system
Focus Functionality Interaction between units System behavior
Dependencies None (isolated) Dependent on modules Dependent on system
11
Tools for Code Coverage
Measure how much of your code is tested.
Popular Tools:
Python: pytest-cov.
JavaScript: Istanbul (via Jest).
Java: JaCoCo.
Code Coverage Metrics:
Statement, branch, and function coverage.
12
Demo
This demonstration shows:
1. Writing a test for a function.
2. Identifying a bug through the failing test.
3. Fixing the code and re-running the test to confirm the fix.
13
14
15
16
17
18
Key Takeaways
● Unit tests ensure your code is robust and maintainable.
● They save time and effort by catching issues early.
● Good tests are simple, isolated, and cover edge cases.
● Embrace automated testing as part of your development workflow.
19
Thank You
Any questions?
20

Presentation on Unit Test with Python and Pytest

  • 1.
    Introduction to Unit Testing Presentedby Md. Abdullah Al Arif One Direction Companies Limited Enhancing Code Reliability One Test at a Time 1
  • 2.
    Table of contents 01What is unit test 02 Why Unit Testing is Important ? 03 Key Characteristics of Unit Tests 04 Best Practices for Writing Unit Tests 05 Common Unit Testing Frameworks 06 Structure of a Unit Test 07 Best Practices for Writing Unit Tests 08 Mocking and Stubbing 09 Challenges in Unit Testing 10 Unit Testing vs Other Types of Testing 11 Tools for Code Coverage 12 Demo 13 Key Takeaways 14 Q&A 2
  • 3.
    What is UnitTest Testing individual units or components of a software application to ensure they work as expected. Focus: Smallest testable parts, like functions or methods. Goal: Identify and fix bugs early in development. 3
  • 4.
    Why Unit Testingis Important ? ● Ensures code reliability and correctness. ● Helps detect bugs early in the development cycle. ● Simplifies debugging by isolating the problematic unit. ● Improves code quality and maintainability. 4
  • 5.
    Key Characteristics ofUnit Tests Isolated: Tests a single "unit" without external dependencies. Automated: Run tests programmatically. Repeatable: Consistent results every time. Fast: Quick execution to maintain workflow efficiency. 5
  • 6.
    Common Unit TestingFrameworks For Python: ● unittest (built-in). ● pytest (popular and user-friendly). For Java: ● JUnit. For JavaScript: ● Jest, Mocha. Others: ● NUnit (C#), PHPUnit (PHP), etc. 6
  • 7.
    Structure of aunit test ● Setup: Prepare the environment and inputs. ● Execution: Call the unit with the prepared inputs. ● Assertion: Check if the output matches expected results. ● Teardown: Clean up (optional). 7
  • 8.
    Best Practices forWriting Unit Tests ● Write tests for edge cases and common use cases. ● Use descriptive names for test methods. ● Keep tests simple and focused on one functionality. ● Mock external dependencies (e.g., databases, APIs). ● Continuously run tests during development. 8
  • 9.
    Mocking and Stubbing Mocking:Replacing a real object with a "fake" version to isolate the unit under test. Stubbing: Providing pre-defined responses from dependencies. Example: Testing a function that calls an external API by mocking the API response. 9
  • 10.
    Challenges in UnitTesting ● Testing private or complex functions. ● Dependency on legacy code with poor testability. ● Writing tests for highly coupled or poorly designed code. ● Balancing test coverage vs. development time. 10
  • 11.
    Unit Testing vsOther Types of Testing Aspect Unit Testing Integration Testing System Testing Scope Smallest unit Modules/components Whole system Focus Functionality Interaction between units System behavior Dependencies None (isolated) Dependent on modules Dependent on system 11
  • 12.
    Tools for CodeCoverage Measure how much of your code is tested. Popular Tools: Python: pytest-cov. JavaScript: Istanbul (via Jest). Java: JaCoCo. Code Coverage Metrics: Statement, branch, and function coverage. 12
  • 13.
    Demo This demonstration shows: 1.Writing a test for a function. 2. Identifying a bug through the failing test. 3. Fixing the code and re-running the test to confirm the fix. 13
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    Key Takeaways ● Unittests ensure your code is robust and maintainable. ● They save time and effort by catching issues early. ● Good tests are simple, isolated, and cover edge cases. ● Embrace automated testing as part of your development workflow. 19
  • 20.