I'm pretty new to pytest. I've built an app and I want to test some of its underlying classes. Suppose one such class looks something like this:
from lib_a import NewInstance
from module_1 import initial_dict
from typing import Optional, Dict
class DataHandler:
def __init__(self, att1: str) -> None:
self.cg = NewInstance()
self.att0 = att0
self.att1 = att1
self.prices = Optional[Dict[str, float]] = None
self.result_dict: Dict[str, float] = {}
def _get_current_prices(self) -> None:
raw = self.cg.get_prices(self.att0, self.att1)
# self.prices = some operation on raw
def calc_answer(self) -> None:
self._get_current_prices()
for key, value in initial_dict.items():
# result_dict[key] = further operations...
Here, lib_a
is a pip-installed library that has a class called NewInstance
(with one of its methods being get_prices()
) that I am using. Basically it is responsible for getting information from the web using requests
etc.
In addition, initial_dict
is a dictionary that I'm importing from another module.
I did some reading about testing and I have a basic understanding of how to test single functions, but when it comes to classes I am pretty confused as to how to do that exactly. I want to test:
- The
_get_current_prices()
method using a mocked.get_prices()
response. - The
calc_answer()
method using the result of (1) and a mockedinitial_dict
.
You can assume that all the # .. operations
are some sort of basic math operations on the relevant defined variables.
How does my test file should look to test this class correctly with pytest (I am getting into the habit of using pytest as it is more recommended from what I have read so far).
Aucun commentaire:
Enregistrer un commentaire