|
| 1 | +Automated testing is the practice of using software tools to automatically run tests on a software application or system, |
| 2 | +rather than relying on manual testing by humans. It is considered an essential part of software development as it |
| 3 | +helps increase productivity, ensure quality and performance goals are met, and provide faster feedback loops to developers. |
| 4 | +Automated tests can include various types such as unit tests, integration tests, end-to-end tests, and more. |
| 5 | + |
| 6 | +While setting up automated tests can be tedious, the benefits of increased test coverage and productivity make it an important aspect of software development. |
| 7 | +Ellar aims to encourage the use of development best practices, including effective testing, by providing various features to assist developers and teams in creating and automating tests. |
| 8 | +These features include: |
| 9 | + |
| 10 | +- automatically generated default unit tests files for components testing |
| 11 | +- offering a util, `TestClientFactory`, that constructs an isolated module/application setup |
| 12 | +- making the Ellar dependency injection system accessible in the testing environment for convenient component mocking. |
| 13 | + |
| 14 | +Ellar is compatible with `unittest` and `pytest` testing frameworks in python but in this documentation, we will be using `pytest`. |
| 15 | + |
| 16 | +## Getting started |
| 17 | +You will need to install `pytest` |
| 18 | + |
| 19 | +```shell |
| 20 | +pip install pytest |
| 21 | +``` |
| 22 | + |
| 23 | +## Unit testing |
| 24 | +In the following example, we test two classes: `CarController` and `CarRepository`. For this we need to use `TestClientFactory` to build |
| 25 | +them in isolation from the application since we are writing unit test. |
| 26 | + |
| 27 | +Looking at the `car` module we scaffolded earlier, there is a `tests` folder provided and inside that folder there is `test_controllers.py` module. |
| 28 | +We are going to be writing unit test for `CarController` in there. |
| 29 | + |
| 30 | +```python |
| 31 | +# project_name/car/tests/test_controllers.py |
| 32 | +from unittest.mock import patch |
| 33 | + |
| 34 | +from project_name.apps.car.controllers import CarController |
| 35 | +from project_name.apps.car.schemas import CreateCarSerializer, CarListFilter |
| 36 | +from project_name.apps.car.services import CarRepository |
| 37 | + |
| 38 | + |
| 39 | +class TestCarController: |
| 40 | + def setup(self): |
| 41 | + self.controller: CarController = CarController(repo=CarRepository()) |
| 42 | + |
| 43 | + async def test_create_action(self, anyio_backend): |
| 44 | + result = await self.controller.create( |
| 45 | + CreateCarSerializer(name="Mercedes", year=2022, model="CLS") |
| 46 | + ) |
| 47 | + |
| 48 | + assert result == { |
| 49 | + "id": "1", |
| 50 | + "message": "This action adds a new car", |
| 51 | + "model": "CLS", |
| 52 | + "name": "Mercedes", |
| 53 | + "year": 2022, |
| 54 | + } |
| 55 | + |
| 56 | + @patch.object(CarRepository, 'get_all', return_value=[dict(id=2, model='CLS',name='Mercedes', year=2023)]) |
| 57 | + async def test_get_all_action(self, mock_get_all, anyio_backend): |
| 58 | + result = await self.controller.get_all(query=CarListFilter(offset=0, limit=10)) |
| 59 | + |
| 60 | + assert result == { |
| 61 | + 'cars': [ |
| 62 | + { |
| 63 | + 'id': 2, |
| 64 | + 'model': 'CLS', |
| 65 | + 'name': 'Mercedes', |
| 66 | + 'year': 2023 |
| 67 | + } |
| 68 | + ], |
| 69 | + 'message': 'This action returns all cars at limit=10, offset=0' |
| 70 | + } |
| 71 | +``` |
| 72 | +In example above, we aren't really testing anything Ellar-specific. Notice that we are not using dependency injection; rather, |
| 73 | +we pass an instance of `CarController` to our `CarRepository`. This type of testing, where we manually instantiate the classes being tested, is commonly referred to as **isolated testing** because it is framework-independent |
1 | 74 | ## Using TestClientFactory
|
2 | 75 | ## Controller Testing
|
3 | 76 | ## Module Testing
|
|
0 commit comments