Class Test

We can also write our test as classes. For example:

├── requirement.txt
├── src
│   ├── __init__.py
│   ├── main.py
│   └── services
│       ├── Database.py
│       ├── MyService.py
│       └── __init__.py
└── tests
    ├── __init__.py
    ├── main_test.py
    └── services
        ├── MyService_test.py
        └── __init__.py

In src/services/MyService.py

from typing import *
from .Database import Database

class MyService:
    def __init__(self, database: Database):
        self.name = "My Service"
        self.database = database
       
    def load_user(self, name: str):
        self.database.query(f"select * from user where name = {name}")

In src/services/Database.py

from typing import *

class Database:
    def query(self, queryString: str):
        print("Performing query: ", queryString)

For the test file in tests/services/MyService_test.py we can have

from src.services.MyService import MyService
from src.services.Database import Database

class TestMyService:
    def setup_method(self, method):
        self.app = MyService(Database())

    def teardown_method(self, method):
        del self.app
        
    def test_load_user_should_success(self):
        actual = self.app.load_user("Austin")
        assert actual is True