February 2022
Intermediate to advanced
274 pages
6h 28m
English
Let’s take a look at how to test config:
| | @app.command() |
| | def config(): |
| | """List the path to the Cards db.""" |
| | with cards_db() as db: |
| | print(db.path()) |
The cards_db() is a context manager that returns a cards.CardsDB object. The returning object is then used as db to call db.path(). So we have two things to mock: cards.CardsDB and one of its methods, path().
We’ll start with the class:
| | def test_mock_CardsDB(): |
| | with mock.patch.object(cards, "CardsDB") as MockCardsDB: |
| | print() |
| | print(f" class:{MockCardsDB}") |
| | print(f"return_value:{MockCardsDB.return_value}") |
| | with cards.cli.cards_db() as db: |
| | print(f" object:{db}") |
This is ...
Read now
Unlock full access