Casie Bao


All the lights we cannot see


Testing in Python

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 by pytest -v -m webtest
  • Or the inverse, running all tests except the webtests by pytest -v -m "not webtest".
Latest Articles

DTO

DTO stands for “Data Transfer Objects”. It is a design pattern for a representation of data, it can be formatted as JSON, XML, etc. It differs from JSON as JSON is a type of serialization and DTO i...…

Read More
Eariler Articles

MongoDB

MongoDB NoSQL mongodb+srv://<username>:<pwd>@<host>/<database>. The hostname does not necessarily imply any cluster configuration.IndexCollection Scan vs Index Scan Colle...…

Read More