Pytest Intro Install pytest.
Go to utils.py
(implementation of Task 2b). Explore the code.
Patch
Patching will be used for the function utils.api_get_data_from_db
to return static value.
Object that class has a direct relationship with.
Class and directed object type are coupled.
Class could depend on another class, because it has an attribute of that type
or becauce object of that type is passed as a parameter to method,
or because the class inherits.
Inheritance introduce the coupling that is hard to remove.
Composition is always a better choice then inheritance
.
Composition and Inheritance
Dependency Injection
is a design pattern.
The idea is that,
if the class uses an object of the certain type
we are not making that class responsible for that object.
Because it shifts the responsibility of creating to another class
dependency injection
is an inversion of control technique.
Big advanatge of dependency injeciton is that your code is much easier to test.
Dependency inversion
is a prinicple in SOLID
design principles.
Decoupled concrete classes uses abstractions, abstract classes, interfaces etc.
Because you need a class in order to construct the object.
This also means that dependecy inversion
is possible when you
separate creation from use.
Without Dependency Injection
there is no Dependency Inversion
.
Go to file before.py
.
Check test_authorizer.py
for scucess fail.
It is hard to test PaymentProcessor
for success and fail,
because it creates authoriser object, calls methods and
checks wheater is authorized or not.
Problem is that the pay method is responsible for creating objects which means we can NOT create it in test, sets to true and pass to the pay method.
Unit test has a mock method that allows you to mock object that are used in functions and replace that objects in those functions. IT IS HARD.
That's where DEPENDENCY INJECTION
pattern comes to play.
Introduce dependency injection to ehnace testability and later dependecy inversion.
We can introduce dependency in
parameter of the method
or
initializer in the class
.
The advantage of the second is that we can use it in different places.
Remove from pay
function, the generation of sms,
because it is not its responsibility.
Go to with_dep_inj_test.py
.
Suggestion
:
Test how the class is created and init.
https://coverage.readthedocs.io/en/7.2.7/
coverage html
coverage run test.py
coverage html
check index.html
in html reader.
We create a layer between that allows us to separate the payment process from authorization much clenanly.
Beacuse the payment processor only calls two methods it does not need to
know we are dealing with AuthorizerSMS
.
We can give him an abstract class, that will have the abstract methods it needs.
Beacuse we added this abstract class, we introduced two abstract methods which are not tested, which is true. On the other habd is useless to test those methods because they are abstract.
Coeverage ingores the method to test
if we replace pass
with doctring.
Is important, allows to separate Authorizer and Payment Processor, do not need to know much about them selfs.
Configuration https://vcrpy.readthedocs.io/en/latest/configuration.html
Convert the bytes
content to json
.
Assert the first element login
and id
.
Compare time with api response and without.
Use the url https://api.github.com/users/hadley/orgs
.