pytest
- library to write tests with powerful fixtures and parametrization featurespydantic
- runtime data validation (similar to @dataclass)Flask
- to create RESTful APIsqlite3
- database
- Configure pytest and project structure
- Define database models with pydantic
- Use pytest fixtures for managing test state and performing side effects
- Verify JSON responses against JSON Schema definitions
- Organize database operations with commands (modify state, has side effects) and queries (read-only, no side effects)
- Unit tests (to test queries and commands), integration (to test API endpoints), and end-to-end tests with pytest
- Generate a coverage report
- Create a test framework:
commands.py
, CreateArticleCommandqueries.py
, ListArticlesQuery, GetArticleByIDQuery, GetArticleByTitleQuery
- Create article
- TC1: Create article -> Expected: all data fields match
- NTC2: Create article with wrong email format (without @) -> Expected: FAIL
- NTC3: Create article with wrong title format -> Expected: FAIL
- NTC4: Create article which already exists -> Expected: AlreadyExists Exception
- Articles can be fetched (select by ID or Title) 2. TC5: Add an article to DB, then query select by ID 3. NTC6: Get article by ID which doesn't exist, empty ID field, int data type -> Expected: models.NotFound 4. NTC7: Get article by title which doesn't exist, same as NTC7
- Articles can be listed (select all)
- TC8: Add articles then list articles
- E2E PI test
- Create a new article
- List articles
- Get the first article from the list
/create-article/
- creates a new article/articles/
- retrieve all articles/article/<article_id>/
- fetch a single article,article_id
is string type
python -m pytest -v --tb=no tests/
- To run E2E test use
python src/init_db.py
andFLASK_APP=src/app.py python -m flask run
- If you got a problem use
export PYTHONPATH=$PYTHONPATH:$PWD
- And,
python -m pytest tests -m 'e2e'
- If you got a problem use
- Acknowledgment to the author of this article!
- How to set up env. vars in Python
- More Pytest plugins
- Add a hook to treat
XFAIL
asPASS
- Add Jenkins CI pipeline with GitHub Webhook
- Refactor
models.py
, with SQL queries in different file - Set up logging
- Extend functionality of framework and add more E2E tests