{ "info": { "author": "Chris L Barnes", "author_email": "barnesc@janelia.hhmi.org", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "# tempcase\n\n[![Build Status](https://travis-ci.org/clbarnes/tempcase.svg?branch=master)](https://travis-ci.org/clbarnes/tempcase)\n\nUtilities for handling python test cases with temporary directories and files, for people using `pyunit`/`unittest`.\n\nSupports python 2.7 and 3.4+.\n\nN.B. the existence of this library is **not** an endorsement of `unittest`. \nUse [`pytest`](https://docs.pytest.org/en/latest/) if you want a powerful, modern, pythonic testing paradigm. \nPlease.\n\n## Motivation\n\nUnit tests should be as isolated as possible, but when testing a file-generating method, it is often inconvenient to \nmanually handle separate output directories with informative names and away from the code.\n\n`pytest` solves this easily with the `tmpdir` fixture, but `unittest` has no such utility.\n\n`tempcase` provides a base class for `unittest`-style test cases with ergonomic methods for creating temporary \ndirectories as required, with automatic cleanup which can be disabled for debugging purposes.\n\n## Installation\n\n```bash\npip install tempcase\n```\n\n## Usage\n\n```python\nimport os\n\nimport tempcase\n\n\nclass MyTestCase(tempcase.TempCase):\n _project_name = 'mylibrary'\n\n def test_creates_file(self):\n \"\"\"\n Test that ``my_file.txt`` is successfully created.\n The first call to ``path_to`` for a ``TestCase`` will create a directory in your default temp directory, \n which has the name of the project as defined above, the name of the ``TestCase``, a timestamp, and a random\n alphanumeric string.\n The first call to ``path_to`` for a test method will create a subdirectory within that, named for the \n test method.\n The test method directory and its contents will be deleted by ``tearDown``.\n The ``TestCase`` directory, if empty, will be cleaned up by ``tearDownClass``.\n \"\"\"\n fpath = self.path_to('my_file.txt') # os.path.join-like syntax\n open(fpath, 'w').close()\n self.assertTrue(os.path.isfile(fpath))\n\n def test_something_else(self):\n \"\"\"No unnecessary directories are created\"\"\"\n self.assertTrue(True)\n\n def test_creates_file_no_cleanup(self):\n \"\"\"\n Setting ``self._cleanup = False`` anywhere in a test method will disable cleanup just for that method, \n allowing you to look at the output for debugging purposes.\n The containing ``TestCase`` directory will also not be deleted.\n \"\"\"\n fpath = self.path_to('my_other_file.txt')\n open(fpath, 'w').close()\n self.assertTrue(os.path.isfile(fpath))\n self._cleanup = False\n\n def tearDown(self):\n \"\"\"Be sure to call the super() tearDown if you override it! Same goes for tearDownClass.\"\"\"\n super().tearDown() # python 3+\n print(\"I did a tearDown\")\n\n\nclass MyTestCaseWithNoCleanup(tempcase.TempCase):\n _project_name = 'mylibrary'\n _cleanup = False\n\n def test_creates_file(self):\n \"\"\"This will not be cleaned up, by default\"\"\"\n fpath = self.path_to('my_file.txt')\n open(fpath, 'w').close()\n\n def test_creates_file_with_cleanup(self)\n \"\"\"You can clean up individual methods if you like\"\"\"\n self._cleanup = True\n open(self.path_to('my_file.txt'), 'w').close()\n\n```\n\nFor existing projects with an already-fragile inheritance chain above every test case, and local paths defined in\na method body (not in a `setUp`), the `in_tempdir` decorator may be useful. It creates a temporary directory, \nchanges the working directory, and then changes back and cleans up the temp dir after execution.\n\n```python\nimport unittest\nimport tempcase\n\nclass MyOldTestCase(unittest.TestCase):\n @tempcase.in_tempdir('my_project')\n def test_old_code(self):\n \"\"\"\n This method has now be ``os.chdir()``'d into a temporary directory which will be cleaned up.\n The working directory will then automatically switch back to whatever it was before.\n The directory's cleanup cannot be prevented.\n \"\"\"\n open('my_local_file.txt', 'w').close()\n\n _project_name = 'my_project' # can be defined in the class or passed to the decorator\n\n @tempcase.in_tempdir\n def test_slightly_newer_code(self):\n pass\n\n```\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/clbarnes/tempcase", "keywords": "test unittest pyunit tmp temp", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "tempcase", "package_url": "https://pypi.org/project/tempcase/", "platform": "", "project_url": "https://pypi.org/project/tempcase/", "project_urls": { "Homepage": "https://github.com/clbarnes/tempcase" }, "release_url": "https://pypi.org/project/tempcase/1.0.0/", "requires_dist": [ "backports.tempfile (>=1.0) ; python_version < \"3.4\"" ], "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "summary": "Utilities for handling python test cases with temporary directories and files, for people using `pyunit`/`unittest`.", "version": "1.0.0" }, "last_serial": 4847659, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "048acefb0bd1e7c203abceecb05c2cb5", "sha256": "aadc8fafd7fe7c757c92ef0de8767c5dc19b9b84ac09e92be672a2062b8e17a9" }, "downloads": -1, "filename": "tempcase-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "048acefb0bd1e7c203abceecb05c2cb5", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "size": 5388, "upload_time": "2018-04-11T20:16:44", "url": "https://files.pythonhosted.org/packages/dc/84/3f959c403ca4584246aa49488fe2337a9062bdb56153a3531c9491a94a20/tempcase-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "284d0da277d2aafba4c65a1dfb6c7c08", "sha256": "ef83c4a3f167fb02a55d7ae19acb93e65f3654d9f8dc0c4f3ab235dd54b0b400" }, "downloads": -1, "filename": "tempcase-0.1.0.tar.gz", "has_sig": false, "md5_digest": "284d0da277d2aafba4c65a1dfb6c7c08", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "size": 5860, "upload_time": "2018-04-11T20:16:45", "url": "https://files.pythonhosted.org/packages/a8/31/232d55ac0def631e22a4868e1e34cf5db32d1af8ad0a53adbdc7fc03dfc8/tempcase-0.1.0.tar.gz" } ], "1.0.0": [ { "comment_text": "", "digests": { "md5": "e70ca26ef3f061ca5079ba7e1982f9cd", "sha256": "252036002d5c94e3e60dc577d69f8ed7bc597345cc292a63390cbf892674ddfc" }, "downloads": -1, "filename": "tempcase-1.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e70ca26ef3f061ca5079ba7e1982f9cd", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "size": 6351, "upload_time": "2019-02-20T22:41:11", "url": "https://files.pythonhosted.org/packages/91/ea/fc3261679da5577711bd960a1e16a9f42e46e349a479e8a4ab166a71488e/tempcase-1.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c3cb286c6f8b5638f90623e687bc97a7", "sha256": "2f2fb58a598a7d10720906363bece9300ce21a9245ef9e1d8edd978bead4f85a" }, "downloads": -1, "filename": "tempcase-1.0.0.tar.gz", "has_sig": false, "md5_digest": "c3cb286c6f8b5638f90623e687bc97a7", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "size": 6004, "upload_time": "2019-02-20T22:41:12", "url": "https://files.pythonhosted.org/packages/38/0c/2856d0b7d413223c18a16ddacd939d99430a0d0a071b8d7ed44b4a85e626/tempcase-1.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e70ca26ef3f061ca5079ba7e1982f9cd", "sha256": "252036002d5c94e3e60dc577d69f8ed7bc597345cc292a63390cbf892674ddfc" }, "downloads": -1, "filename": "tempcase-1.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e70ca26ef3f061ca5079ba7e1982f9cd", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "size": 6351, "upload_time": "2019-02-20T22:41:11", "url": "https://files.pythonhosted.org/packages/91/ea/fc3261679da5577711bd960a1e16a9f42e46e349a479e8a4ab166a71488e/tempcase-1.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c3cb286c6f8b5638f90623e687bc97a7", "sha256": "2f2fb58a598a7d10720906363bece9300ce21a9245ef9e1d8edd978bead4f85a" }, "downloads": -1, "filename": "tempcase-1.0.0.tar.gz", "has_sig": false, "md5_digest": "c3cb286c6f8b5638f90623e687bc97a7", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", "size": 6004, "upload_time": "2019-02-20T22:41:12", "url": "https://files.pythonhosted.org/packages/38/0c/2856d0b7d413223c18a16ddacd939d99430a0d0a071b8d7ed44b4a85e626/tempcase-1.0.0.tar.gz" } ] }