Package: pytest-fastapi-deps
pytest-fastapi-depsA fixture which allows easy replacement of fastapi dependencies for testingInstallationpipinstallpytest-fastapi-depsor install withPoetrypoetryaddpytest-fastapi-depsUse caseSuppose that you have this fastapi endpoint which has a couple of dependencies:fromfastapiimportDepends,FastAPIapp=FastAPI()asyncdeffirst_dep():return{"skip":0,"limit":100}defsecond_dep():return{"skip":20,"limit":50}@app.get("/depends/")asyncdefget_depends(first_dep:dict=Depends(first_dep),second_dep:dict=Depends(second_dep)):return{"first_dep":first_dep,"second_dep":second_dep}For simplicity, this example holds static dictionaries, but in reality these
dependencies can be anything: dynamic configuration, database information, the
current user's information, etc.If you want to test your fastapi endpoint you might wish to mock or replace these
dependencies with your test code.This is where thefastapi_depfixture comes to play.UsageThe most basic usage is to replace a dependency with a context manager:frommy_project.mainimportapp,first_dep,second_depfromfastapi.testclientimportTestClientclient=TestClient(app)defmy_second_override():return{"another":"override"}deftest_get_override_two_dep(fastapi_dep):withfastapi_dep(app).override({first_dep:"plain_override_object",second_dep:my_second_override,}):response=client.get("/depends")assertresponse.status_code==200assertresponse.json()=={"first_dep":"plain_override_object","second_dep":{"another":"override"},}Note how easy it is: you add thefastapi_depfixture, initialize it with the fastapiappand send a dictionary of overrides: the keys are the original functions while the
values are plain objects that would be returned or replacement functions that would be
called.If your use case is to replace the dependencies for the entire duration of your test,
you can use pytestindirect parametersto simplify the body of your test:importpytestfrommy_project.mainimportapp,first_dep,second_depfromfastapi.testclientimportTestClientclient=TestClient(app)@pytest.mark.parametrize("fastapi_dep",[(app,{first_dep:lambda:{"my":"override"}},)],indirect=True,)deftest_get_override_indirect_dep_param(fastapi_dep):response=client.get("/depends")assertresponse.status_code==200assertresponse.json()=={"first_dep":{"my":"override"},"second_dep":{"skip":20,"limit":50},}You must useindirect=Trueand pass a tuple where the first item is theappand the
second item is the dictionary with replacement functions.You can do more fancy stuff and utilize the nature of nested python context managers:frommy_project.mainimportapp,first_dep,second_depfromfastapi.testclientimportTestClientclient=TestClient(app)deftest_get_override_dep_inner_context(fastapi_dep):withfastapi_dep(app).override({first_dep:lambda:{"my":"override"}}):response=client.get("/depends")assertresponse.status_code==200assertresponse.json()=={"first_dep":{"my":"override"},# overridden"second_dep":{"skip":20,"limit":50},# stayed the same}# add another overridewithfastapi_dep(app).override({second_dep:lambda:{"another":"override"}}):response=client.get("/depends")assertresponse.status_code==200assertresponse.json()=={"first_dep":{"my":"override"},# overridden"second_dep":{"another":"override"},# overridden}# second override is gone - expect that only the first is overriddenresponse=client.get("/depends")assertresponse.status_code==200assertresponse.json()=={"first_dep":{"my":"override"},# overridden"second_dep":{"skip":20,"limit":50},# returned to normal behaviour}# back to normal behaviourresponse=client.get("/depends")assertresponse.status_code==200assertresponse.json()=={"first_dep":{"skip":0,"limit":100},"second_dep":{"skip":20,"limit":50},}📈 ReleasesYou can see the list of available releases on theGitHub Releasespage.We followSemantic Versionsspecification.🛡 LicenseThis project is licensed under the terms of theMITlicense. SeeLICENSEfor more details.📃 Citation@misc{pytest-fastapi-deps,author={Peter Kogan},title={A fixture which allows easy replacement of fastapi dependencies for testing},year={2022},publisher={GitHub},journal={GitHub repository},howpublished={\url{https://github.com/pksol/pytest-fastapi-deps}}}CreditsThis project was generated withpython-package-template