{ "info": { "author": "Austin Bingham", "author_email": "austin.bingham@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "================\n `with_fixture`\n================\n\n`with_fixture` is an experiment in using context-managers naturally\nwith Python `unittest` fixtures.\n\nTest fixtures and context-managers have very similar structures. In\nboth cases, they define bits of code to be run before and after other\nbits of *a priori* unknown code. They are the bread in little code\nsandwiches.\n\nYet, as far as I know, there is no natural way to use them\ntogether. That is, there is no way to use context-managers in - or\nperhaps *as* - test fixtures with the standard `unittest` module. This\nis doubly egregious because context-managers are a central element in\nmodern Python design, and we should be encouraging their use widely.\n\n`with_fixture`, then, is an experiment in making them work together. I\nwant to start to explore the options we have for using\ncontext-managers in test fixtures, with the long-term goal of getting\nsomething into the standard library.\n\nExperiment 1: `with_fixture.TestCase.withFixture()`\n===================================================\n\n`with_fixture` currently implements a single, simple approach to\ncombining context-managers and fixtures. It's a kind of *minimum\nviable product*. The idea is to subclass `unittest.TestCase` and\noverride the `setUp()` and `tearDown()` methods to drive a new\ngenerator method, `withFixture()`.\n\n`withFixture()` must call `yield` once. Everything before the `yield`\nis the equivalent of `setUp()` in `unittest.TestCase`, and everything\nafter the `yield` is equivalent to `tearDown()`. By placing the\n`yield` in a *with-block*, it becomes very natural to use\ncontext-managers in test fixtures.\n\nExample: Temporary file\n-----------------------\n\nHere we use the `tempfile.TemporaryFile` context-manager in `withFixture()`::\n\n class TestBindingToMembers(TestCase):\n TEST_DATA = b'1234567890'\n\n def withFixture(self):\n # This creates a tempfile and binds it to self.f\n with tempfile.TemporaryFile() as self.f:\n # This yield sends control to the test\n yield\n\n # The test has now been executed. Check for the data we\n # expect in the file.\n self.f.seek(0)\n assert(self.f.read() == self.TEST_DATA)\n\n def test_nothing(self):\n self.f.write(self.TEST_DATA)\n\nThis is clearly a bit contrived (it's taken from the somewhat awkward\ntest suite for `with_fixture` itself), but you can see what's going\non.\n\nFuture work\n===========\n\nThe current implementation is small and simple, and there's plenty of\nroom for further experimentation. Areas to investigate include:\n\n - Allowing users to use `setUp()` and `tearDown()` along with\n `withFixture()`. Currently we make that difficult. There are a\n number of interesting problems that arise when you start to think\n about this.\n\n - Integrating an implementation directly into `unittest`.\n\n - Consider alternative names. I thing `withFixture` is pretty good\n and communicates the design intent, but others may find it\n confusing.\n\n - Consider equivalent support for `setUp/tearDownClass()`.\n\n - Do some deeper thinking on exceptions. I *think* the currently\n implementation is pretty sound WRT exceptions (i.e. that setup and\n teardown will get executed correctly in the face of exceptions) but\n I'm not testing it and haven't given it great throught yet.\n\n - Completely different designs. I can imagine completely different\n ways to approach this, and I'm sure others can too.\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/abingham/with_fixture", "keywords": "testing", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "with_fixture", "package_url": "https://pypi.org/project/with_fixture/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/with_fixture/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/abingham/with_fixture" }, "release_url": "https://pypi.org/project/with_fixture/0.1/", "requires_dist": null, "requires_python": null, "summary": "An experiment in using context-managers in unittest fixtures.", "version": "0.1" }, "last_serial": 1096269, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "726f2345e2ac9fba4b74a3a7611187d3", "sha256": "9756ac4bb18153ce2c7d1157761852b57bdeb4fcea50e226169b0bb1c4944d12" }, "downloads": -1, "filename": "with_fixture-0.1.tar.gz", "has_sig": false, "md5_digest": "726f2345e2ac9fba4b74a3a7611187d3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2597, "upload_time": "2014-05-18T15:58:49", "url": "https://files.pythonhosted.org/packages/65/c8/3cf4a8b91f2efc8b68c1e289f64623adc845cb13e355fc408c4ed6940bff/with_fixture-0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "726f2345e2ac9fba4b74a3a7611187d3", "sha256": "9756ac4bb18153ce2c7d1157761852b57bdeb4fcea50e226169b0bb1c4944d12" }, "downloads": -1, "filename": "with_fixture-0.1.tar.gz", "has_sig": false, "md5_digest": "726f2345e2ac9fba4b74a3a7611187d3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2597, "upload_time": "2014-05-18T15:58:49", "url": "https://files.pythonhosted.org/packages/65/c8/3cf4a8b91f2efc8b68c1e289f64623adc845cb13e355fc408c4ed6940bff/with_fixture-0.1.tar.gz" } ] }