Setup/Teardown
✅ Traditional Setup & Teardown
1️⃣ Traditional Setup/Teardown কী?
১. Setup & Teardown কী?
Setup এবং Teardown হলো test execution-এর আগে এবং পরে run হওয়া function/logic।
- Setup: Test run করার আগে প্রয়োজনীয় environment বা objects prepare করা
- Teardown: Test run হওয়ার পরে resources clean করা
Professional use-case:
- Database connection open করা → test শেষে close করা
- API client initialize করা → test শেষে cleanup করা
- Temporary files create করা → test শেষে delete করা
২. Pytest-এ কেন ব্যবহার করা হয়?
- DRY (Don’t Repeat Yourself): বারবার একই code লিখতে হয় না
- Test isolation: প্রতিটি test independent থাকে
- Reusable setup: Multiple test functions একই setup ব্যবহার করতে পারে
- Resource management: DB, file, network, API, sockets safely handle করা
2️⃣ Available Traditional Functions
| Function | কখন Run হয় |
|---|---|
setup_module(module) | Module শুরু হওয়ার আগে |
teardown_module(module) | Module শেষ হওয়ার পরে |
setup_function(function) | প্রতিটি test function এর আগে |
teardown_function(function) | প্রতিটি test function এর পরে |
setup_class(cls) | Test class শুরু হওয়ার আগে |
teardown_class(cls) | Test class শেষ হওয়ার পরে |
3️⃣ Test Lifecycle Overview
ধরো test file:
test_math.py
Execution flow:
setup_module
setup_function
test_1
teardown_function
setup_function
test_2
teardown_function
teardown_module
4️⃣ Module Level Setup & Teardown
Purpose
✔ Heavy resource initialize করার জন্য
Example:
Database start
Docker service start
API server boot
Syntax
def setup_module(module):
pass
def teardown_module(module):
pass
Example
def setup_module(module):
print("\n[MODULE SETUP]")
def teardown_module(module):
print("\n[MODULE TEARDOWN]")
def test_add():
assert 2 + 2 == 4
def test_sub():
assert 5 - 2 == 3
Run Command
pytest -v test_math.py
Output
[MODULE SETUP]
test_add PASSED
test_sub PASSED
[MODULE TEARDOWN]
👉 একবারই run হয়।
5️⃣ Function Level Setup & Teardown
Purpose
✔ Every test independent করা।
Use cases:
Reset variables
Clear cache
Create temporary object
Syntax
def setup_function(function):
pass
def teardown_function(function):
pass
Example
def setup_function(function):
print(f"\nSetup for {function.__name__}")
def teardown_function(function):
print(f"\nCleanup for {function.__name__}")
def test_a():
assert True
def test_b():
assert True
Output
Setup for test_a
PASSED
Cleanup for test_a
Setup for test_b
PASSED
Cleanup for test_b
👉 প্রতিটি test এর আগে ও পরে run হয়।
6️⃣ Class Level Setup & Teardown
Purpose
✔ Multiple tests share same object।
Example:
API client
Browser driver
Service instance
Syntax
class TestSomething:
@classmethod
def setup_class(cls):
pass
@classmethod
def teardown_class(cls):
pass
Example
class TestCalculator:
@classmethod
def setup_class(cls):
print("\n[CLASS SETUP]")
@classmethod
def teardown_class(cls):
print("\n[CLASS TEARDOWN]")
def test_add(self):
assert 1 + 1 == 2
def test_mul(self):
assert 2 * 3 == 6
Run
pytest -v test_math.py
Output
[CLASS SETUP]
test_add PASSED
test_mul PASSED
[CLASS TEARDOWN]
👉 Class প্রতি একবার run হয়।
7️⃣ FULL Example (All Combined)
File: test_full.py
# ---------- MODULE ----------
def setup_module(module):
print("\nMODULE SETUP")
def teardown_module(module):
print("\nMODULE TEARDOWN")
# ---------- FUNCTION ----------
def setup_function(function):
print(f"\nSETUP FUNCTION: {function.__name__}")
def teardown_function(function):
print(f"\nTEARDOWN FUNCTION: {function.__name__}")
# ---------- TEST FUNCTIONS ----------
def test_one():
assert True
def test_two():
assert True
# ---------- CLASS ----------
class TestGroup:
@classmethod
def setup_class(cls):
print("\nCLASS SETUP")
@classmethod
def teardown_class(cls):
print("\nCLASS TEARDOWN")
def test_three(self):
assert True
def test_four(self):
assert True
Run Command
pytest -v test_full.py
Execution Order (Very Important)
MODULE SETUP
SETUP FUNCTION test_one
test_one
TEARDOWN FUNCTION test_one
SETUP FUNCTION test_two
test_two
TEARDOWN FUNCTION test_two
CLASS SETUP
SETUP FUNCTION test_three
test_three
TEARDOWN FUNCTION test_three
SETUP FUNCTION test_four
test_four
TEARDOWN FUNCTION test_four
CLASS TEARDOWN
MODULE TEARDOWN
🔥 এটা interview favourite question।
8️⃣ Real Production Usage (example)
Database Testing
setup_module → start DB
setup_function → insert test data
test → run query
teardown_function → delete data
teardown_module → close DB
API Testing
setup_module → login & token generate
tests → API calls
teardown_module → logout
9️⃣ Important Rules
✅ Function names fixed
✅ pytest automatically detect করে
✅ manually call করা যাবে না
✅ Wrong name দিলে run হবে না
Example ❌
def setup(): # WRONG
🔟 Best Practices (Senior Tester Rules)
✔ Heavy setup → module/class
✔ Light setup → function
✔ Always cleanup resources
✔ Tests must not depend on previous tests
✔ Avoid global state
⚠️ Modern Reality
আজকের professional pytest project-এ:
👉 Traditional setup/teardown legacy support
👉 Modern projects mostly use fixtures
কারণ:
more flexible
dependency injection support
cleaner architecture
কিন্তু concept একই।
🔥 Senior Level Insight
Real enterprise testing lifecycle:
CI Runner Start
setup_module
setup_class
setup_function
test
teardown_function
teardown_class
teardown_module
CI Runner End
যে engineer এটা বোঝে — সে flaky test তৈরি করে না।