{ "info": { "author": "Pedram Hajesmaeeli", "author_email": "pedram.esmaeeli@gmail.com", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: Dynamic Content" ], "description": "DRF Test\n----------\n\n[![Build Status](https://travis-ci.com/pdrum/drftest.svg?branch=master)](https://travis-ci.com/pdrum/drftest)\n\n**DRF Test** is a minimal testing library that aims to facilitate writing **DRY** tests for \ndjango rest framework views. It also [optionally] generates good looking API documentations based\non tests it runs. \n\nOne thing that usually becomes bothering about documentations is that\nthey need to be kept up to date and keeping them up to date takes a lot of effort from development\nteams and more often than not we tend to not keep them up to date and they eventually lose their\nvalue. With **DRF Test** API docs will be generated based on your tests. So as long as your\ntests pass and your code is working, you can be sure that your docs are also up to date!\n\n# Installation\nRun `pip install drftest`. Also Make sure you use `python >= 3.4.0`.\n\n# Links\n**DRF Test** can be accessed using:\n\n* Pypi: https://pypi.org/project/drftest/\n* Repo exemplifying how to use **DRF Test**: https://github.com/pdrum/drftest_example\n* Documentation demo: https://drftest.netlify.com/\n\n# Preparation\nIn order to use `DRF Test` initially you need to take the following four steps:\n\n* Add `drftest` to your list of `INSTALLED_APPS` like:\n```python\nINSTALLED_APPS = [\n ...,\n 'drftest',\n]\n```\n\n* Add `TEST_RUNNER = 'drftest.TestRunner'` to your `settings.py` file.\n\n* **DRF Test** has an interface called `AuthProvider` which can be imported with\n`from drftest import AuthProvider` and looks like\n```python\nclass AuthProvider:\n @abstractmethod\n def set_auth(self, api_client: APIClient, user):\n \"\"\"\n Authenticates user using the given `api_client`\n \"\"\"\n raise NotImplementedError()\n\n def get_auth_headers(self, user):\n \"\"\"\n Returns a dictionary indicating what headers need to be set for authentication\n purpose of given user.\n It is only used in docs.\n \"\"\"\n return {}\n```\n\nIn order to use **DRF Test** you need to make a class that inherits from `AuthProvider`\nand implements its two abstract methods. Then you need to tell **DRFTest** where your \nauth provider lives. For example if the class is called `MyAuthProvider` and resides\nin a module called `my_auth_provider` at the root of your project, you should add the following\nline to your `settings.py` file:\n```python\nDRF_TEST_AUTH_PROVIDER_CLASS = 'my_auth_provider.MyAuthProvider'\n```\n\n* Optionally you can set value of a special variable called `DRF_TEST_DOCS_DIR` in your\nsettings if you do so, then **DRF Test** will create a nice documentation in the directory\nspecified by `DRF_TEST_DOCS_DIR`. In this documentation **DRF Test** will demonstrate what the \ninput and output is for each of your APIs under test. If this \nvariable is not set **DRF Test** will avoid creating docs after tests are run. It's also \na good idea to let `DRF_TEST_DOCS_DIR` be a directory under root of your django project so \nthat it is kept track of using VCS under the same repository as the rest of your code.\n\n# Writing your first test\n**DRF Test** is not just about generating docs but it also helps you not repeat yourself while\nwriting tests for your django views. Writing tests using **DRF Test** is not much different\nfrom writing simple django tests except for a few simple things.\n\nYour test classes should extend either `BaseViewTest` or `BaseAuthenticatedViewTest` and implement\ntheir abstract methods. Each test class tests only a single url which is handled by methods\nof a view class.\n\nFor `BaseViewTest` (`from drftest import BaseViewTest`) you override the following two methods:\n```python\n@abstractmethod\ndef _make_url(self, kwargs=None):\n \"\"\"\n :param kwargs: path parameters in a form which can get passed to django's \n reverse function \n :return: url which is being tested. (Most probably generated using django's reverse \n function)\n \"\"\"\n raise NotImplementedError()\n \n \n@abstractmethod\ndef _get_view_class(self):\n \"\"\"\n :return: The view class which is being tested. For example if MyAwsomeView is being\n tested, this method should look like `return MyAwsomeView` \n \"\"\"\n raise NotImplementedError\n```\n\nAlso for tests where the url being tested has some path parameters you should override the\nfollowing method as well. (It defaults to returning `None`.)\n```python\ndef _get_default_url_kwargs(self):\n \"\"\"\n :return: A dictionary where the keys are name of path parameters and the values\n are their default values. It doesn't matter if the values are not meaningful\n defaults. The output is just used by tests generated by drftest which check\n whether or not things like url mapping, etc are working and your view is working.\n \"\"\"\n return None\n```\n\nIn case the view class being tested has some `permission_classes` your tests can inherit\n`BaseAuthenticatedViewTest` (`from drftest import BaseAuthenticatedViewTest`) instead of \n`BaseViewTest`. This way you should also override a fourth method which looks like:\n```python\n@abstractmethod\ndef _get_permission_classes(self):\n \"\"\"\n Should return a list of permission classes. drftest generates a testcase to check if\n result of calling this method matches those specified by `permission_classes` attribute\n of your class. This way you can test your permission classes separately and in other\n tests just use drftest's auto-generated testcases to check if correct permissions are set. \n \"\"\"\n raise NotImplementedError()\n```\n\nFinally whenever you need to make a request use `self._get_for_response`, `self._post_for_response`,\n`self._put_for_response`, `self._head_for_response` or `self._patch_for_response`. Here's what each \nof their parameters does (None of them are required. They all have default values.):\n\n* **User** is user of the request. User will be authenticated using `AuthProvider` you wrote earlier.\nIf user is not given, request will be sent using an anonymous user.\n* **data** is body of requests (in case of things like POST) or query parameters (in case of\nthings like GET or DELETE that do not have a body.)\n* **url_kwargs** is a dictionary specifying path parameters to use.\n* **format** defaults to `json`. Can also be set to `multipart`. Docs will not get generated\nfor multipart requests.\n* **docs** is a boolean whether or not the request should appear in docs and defaults to `True` \n* **extra [as kwargs]** Headers can be passed as kwargs.\n\n# Using generated docs\nDocs are generated to be used with [mkdocs](https://www.mkdocs.org/). Installing it takes only\na single command. After installing it you can use `mkdocs serve` to run the docs and then\nvisit them at `http://localhost:8000`. You can \nalso generate a static website out of it very easily. \nFor more information visit [mkdocs](https://www.mkdocs.org/).\n\nHere's how you can install mkdocs onn mac and ubuntu respectively.\n\n```\nbrew install mkdocs\nsudo apt-get install mkdocs-doc\n``` \n\n# Advantages\n* You will not repeat things like writing url of test, name of class being tested, \nauthentication mechanism for user sending requests in your tests, etc in each and every test.\n* Tests of your view will all look quite similar even if multiple people are working on them \nsimultaneously.\n* Docs will get generated from your tests.", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/pdrum/drftest", "keywords": "", "license": "MIT License", "maintainer": "", "maintainer_email": "", "name": "drftest", "package_url": "https://pypi.org/project/drftest/", "platform": "", "project_url": "https://pypi.org/project/drftest/", "project_urls": { "Homepage": "https://github.com/pdrum/drftest" }, "release_url": "https://pypi.org/project/drftest/1.0.3/", "requires_dist": null, "requires_python": "", "summary": "drftest is a minimal testing library that aims to facilitate writing tests for django rest framework views. It also [optionally] generates good looking API documentations based on tests it runs.", "version": "1.0.3" }, "last_serial": 4665954, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "f97d01c3aa97431a418cb40daaec544b", "sha256": "6c3bad6ba95e3e50bde9bbac840a1ba080797e52a865d9d48b4d2dbc81ea3db0" }, "downloads": -1, "filename": "drftest-1.0.0.tar.gz", "has_sig": false, "md5_digest": "f97d01c3aa97431a418cb40daaec544b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12125, "upload_time": "2018-11-18T00:29:07", "url": "https://files.pythonhosted.org/packages/d2/86/b75a33484aed0e8b92fee69352abe424b48ad7b41eb63a184adcc197a577/drftest-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "a2d2ed95522efd0f5d990a7800e56881", "sha256": "5948455d6ff5734131dcff6ca4f5e8cfc64c476c2200e36ebab2f4b740e5f781" }, "downloads": -1, "filename": "drftest-1.0.1.tar.gz", "has_sig": false, "md5_digest": "a2d2ed95522efd0f5d990a7800e56881", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12498, "upload_time": "2018-11-28T14:13:04", "url": "https://files.pythonhosted.org/packages/4a/fa/005f331a1f5d4cb6ef3563c92c3c6b7ea76a6616eb452972a21ab5e09d80/drftest-1.0.1.tar.gz" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "a4e9ff223d796b002c75d619d60c8dee", "sha256": "de2fd92061cfe0df74878d5b98e3c691fcb0fdcf87aecf416d42b15f2d8cae92" }, "downloads": -1, "filename": "drftest-1.0.2.tar.gz", "has_sig": false, "md5_digest": "a4e9ff223d796b002c75d619d60c8dee", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15217, "upload_time": "2018-12-17T17:05:40", "url": "https://files.pythonhosted.org/packages/a5/ca/68119910428aecfda7050b6830fa193b412b46e44eb683ff839419345da2/drftest-1.0.2.tar.gz" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "2f356f41044528a4d420dd5f4613e3e0", "sha256": "845fcaed9b4269f10d8a387625ed1e7b955f221f5871d873e973ccb53f88d6a8" }, "downloads": -1, "filename": "drftest-1.0.3.tar.gz", "has_sig": false, "md5_digest": "2f356f41044528a4d420dd5f4613e3e0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15368, "upload_time": "2019-01-06T16:04:20", "url": "https://files.pythonhosted.org/packages/8d/2f/e62893b42a303f080e61bc44fb09891a3398f92aba304a26522b9e08a689/drftest-1.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2f356f41044528a4d420dd5f4613e3e0", "sha256": "845fcaed9b4269f10d8a387625ed1e7b955f221f5871d873e973ccb53f88d6a8" }, "downloads": -1, "filename": "drftest-1.0.3.tar.gz", "has_sig": false, "md5_digest": "2f356f41044528a4d420dd5f4613e3e0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15368, "upload_time": "2019-01-06T16:04:20", "url": "https://files.pythonhosted.org/packages/8d/2f/e62893b42a303f080e61bc44fb09891a3398f92aba304a26522b9e08a689/drftest-1.0.3.tar.gz" } ] }