{"id":20974396,"url":"https://github.com/septa-ma/django-pytest","last_synced_at":"2025-04-12T01:35:43.098Z","repository":{"id":138826932,"uuid":"603540071","full_name":"septa-ma/Django-Pytest","owner":"septa-ma","description":"writing test in Django with Pytest","archived":false,"fork":false,"pushed_at":"2023-03-17T22:27:37.000Z","size":14,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T21:15:50.066Z","etag":null,"topics":["github-actions","github-automated","pipeline","pytest","pytest-django","python","testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/septa-ma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-18T20:24:39.000Z","updated_at":"2023-12-12T16:17:13.000Z","dependencies_parsed_at":"2023-05-01T03:16:14.978Z","dependency_job_id":null,"html_url":"https://github.com/septa-ma/Django-Pytest","commit_stats":null,"previous_names":["septa-ma/django-pytest"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/septa-ma%2FDjango-Pytest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/septa-ma%2FDjango-Pytest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/septa-ma%2FDjango-Pytest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/septa-ma%2FDjango-Pytest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/septa-ma","download_url":"https://codeload.github.com/septa-ma/Django-Pytest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248504724,"owners_count":21115196,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["github-actions","github-automated","pipeline","pytest","pytest-django","python","testing"],"created_at":"2024-11-19T04:28:38.227Z","updated_at":"2025-04-12T01:35:43.066Z","avatar_url":"https://github.com/septa-ma.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pytest\n\n# HOW TO CONFIGURE AND USE DJANGO PYTEST?\n\n# after makeing project and app:\n - 1- make tests folder \n - 2- install pytest -\u003e pip install pytest-django\n - 3- make pytest.ini file and add:\n   -  [pytest]\n   -  DJANGO_SETTINGS_MODULE = test.settings\n   -  python_files = tests.py test_*.py *_tests.py\n\n - 4- write your tests\n - 5- for running:\n   - pytest                                 -\u003e run all tests\n   - pytest a_directory                     -\u003e directory\n   - pytest test_something.py               -\u003e tests file\n   - pytest test_something.py::single_test  -\u003e single test function\n\n *************************************************************** \n\n# a pattern for writing tests\n# 1- Arrange:\n - prepare any state you need to perform the action you want to try.\n - trying to prepare and ready test to perform the action at the second stage the act.\n + does the test require any objects? or sepecially settings? \n + does it need any DB at all to run the test? \n + do we need to login in any web, app? \n + do we need to connect to any API?\n\n# 2- Act:\n - we're performing an action.\n - this could calling a functional method or maybe calling a REST-API or inteacting with a web page.\n - typically this step should listen to some sort of response\n + this step should have some sort of response \n + hhtp response or data which return from an API\n\n# 3- Assert\n - assert expected outcomes\n - take the response from the act step and test it agenst the expected outcome of performing the action\n - if we were mean to return some data from act step then the assert is going to test to see if that data was indeed returned.\n - if we do assert the action or the act and we have return the expected outcome then return True.\n\n# pytest fixtuers:\n - are functions\n - run before/after each test function to which the fixture is applied.\n + we use fixtures to collect data from DB or for prepare a DB connection so then we can perform a function in the act to return response and then we can assert the response to check to see if we return an expected outcome.\n + fixtures are used to feed data to the tests such as DB connections, URLs to test and input data.\n\n**If the project is in a large scale, it becomes hard to manage tests with fixtures. In this case it is better to use factory boy.**\n\n# Factory Boy?\n- fixture replacement tool\n- factories are define in a clean and readable manner.\n- easy to use factories for complex objects.\n- class-based approach\n    - SubFactories\n    - ForeignKey, reverse ForeignKey, ManyToMany\n\n# How to use factory boy?\n- install packages:\n    - pip install pytest-factoryboy\n    - pip install Fake \n        - for more information about Fake check this repository:\n        **https://github.com/septa-ma/Django-Generate-Fake-Data.git**\n- build classes -\u003e connect the class in factories to a model.\n- register the factory\n\n**Factory boy can provide us to built-in strategies.**\n- If we want factories to produce an  object, use build.\n  - EX: user_factory.build()\n- But if we want to save data to our test db, here we use create.\n  - need db access -\u003e @pytest.mark.django_db\n  - EX: user_factory.create() \n    \n**pytest parametrization allows us to define multiple sets of arguments and fixtures and test functional class**\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsepta-ma%2Fdjango-pytest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsepta-ma%2Fdjango-pytest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsepta-ma%2Fdjango-pytest/lists"}