Mike Cohen’s Test Automation Pyramid
- Bottom to top is Unit tests, Service/API layer tests, User Interface tests
- Unit testing and API testing: unittest, pytest, nose
- UI testing: Selenium and Python
Selenium
- Selenium automates browsers
- used for web UI automation- the automatic execution of the actions performed in a web browser window
- Install Selenium
pip install selenium
Pytest
Pytest can be used in many cases in the project for unit testings. For example, in MAFEA, we use it to verify our schema written in pydantic classes. In MongoDB M220 class, we use the pytest to verify our functin results.
The example below shows a unit test using pytest:
import pytest
from mflix.db import get_movies_by_country
@pytest.mark.projection
def test_basic_country_search_db(client):
countries = ['Kosovo']
result = get_movies_by_country(countries)
assert len(result) == 2
Marking test functions and selecting them for a run
import pytest
@pytest.mark.webtest
def test_send_http():
pass # perform some webtest test for your app
def test_something_quick():
pass
def test_another():
pass
- when test, you could only run tests marked with
webtest
bypytest -v -m webtest
- Or the inverse, running all tests except the webtests by
pytest -v -m "not webtest"
.