{"action":"created","issue":{"url":"https://api.github.com/repos/pytest-dev/pytest-xdist/issues/18","labels_url":"https://api.github.com/repos/pytest-dev/pytest-xdist/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/pytest-dev/pytest-xdist/issues/18/comments","events_url":"https://api.github.com/repos/pytest-dev/pytest-xdist/issues/18/events","html_url":"https://github.com/pytest-dev/pytest-xdist/issues/18","id":120046559,"number":18,"title":"Use fixtures to better distribute tests via load scheduling","user":{"login":"nchammas","id":1039369,"avatar_url":"https://avatars.githubusercontent.com/u/1039369?v=3","gravatar_id":"","url":"https://api.github.com/users/nchammas","html_url":"https://github.com/nchammas","followers_url":"https://api.github.com/users/nchammas/followers","following_url":"https://api.github.com/users/nchammas/following{/other_user}","gists_url":"https://api.github.com/users/nchammas/gists{/gist_id}","starred_url":"https://api.github.com/users/nchammas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nchammas/subscriptions","organizations_url":"https://api.github.com/users/nchammas/orgs","repos_url":"https://api.github.com/users/nchammas/repos","events_url":"https://api.github.com/users/nchammas/events{/privacy}","received_events_url":"https://api.github.com/users/nchammas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-12-02T22:07:05Z","updated_at":"2015-12-02T23:40:52Z","closed_at":null,"body":"xdist currently supports [2 ways of distributing tests](https://github.com/pytest-dev/pytest-xdist/blob/14f39a7c4dc348a0c82021d07ce161186f016b80/xdist/plugin.py#L25-L32) across a number of workers:\r\n\r\n1. [\"Each\" Scheduling](https://github.com/pytest-dev/pytest-xdist/blob/d6dfee1cf2768fcf7ffe30f16fc73a1c9fc72f62/xdist/dsession.py#L13-L25): Given a set of tests, \"each\" scheduling sends each test in the set to each available worker.\r\n2. [Load Scheduling](https://github.com/pytest-dev/pytest-xdist/blob/d6dfee1cf2768fcf7ffe30f16fc73a1c9fc72f62/xdist/dsession.py#L138-L152): Given a set of tests, load scheduling distributes the tests among the available workers. Each test runs only once, and xdist tries to give each worker the same amount of work.\r\n\r\n\r\n## Problems with Load Scheduling's current implementation\r\n\r\nThe current load scheduling implementation distributes tests naively across the workers. Often this means that two tests which depend on the same [fixture](http://pytest.org/latest/fixture.html) get assigned to different runners, and the fixture has to be created on each runner.\r\n\r\nThis is a problem. Fixtures often capture expensive operations. When multiple tests depend on the same fixture, the author typically expects the expensive operation represented by that fixture to happen only once and be reused by all dependent tests. When tests that depend on the same fixture are sent to different workers, that expensive operation is executed multiple times. This is wasteful and can add significantly to the overall testing runtime.\r\n\r\nOur goal should be to **reuse the existing pytest concept of the fixture to better distribute tests and reduce overall testing time, preferably *without* adding new options or APIs.** This benefits the most users and strengthens pytest's declarative style.\r\n\r\n\r\n## Proposed solution\r\n\r\nWe can solve this problem in 3 phases:\r\n\r\n1. **Let's formalize the concept of a \"test chunk\" or \"test block\".** A test chunk is a group of tests that always execute on the same worker. This is an internal xdist abstraction that the user normally doesn't have to know about.\r\n\r\n  The master will only send complete test chunks to workers, not individual tests. Initially, each test will be assigned to its own chunk, so this won't change xdist's behavior at first. But it will pave the way for us to chunk tests by their attributes, like what fixtures they depend on.\r\n  \r\n  Once we have this internal abstraction, we can optionally also expose a hook that lets users define their own chunking algorithm to replace the initial default of \"1 test -> 1 chunk\". The hook won't be very useful until more information about each test is made available, which brings us to the next phase.\r\n\r\n2. **We need to pass in additional information to the xdist master about the tests it's running so it can better distribute them.** Specifically the master needs to be able to identify unique instances of every fixture that each test depends on. Tests that depend on distinct fixture instances can be assigned to different chunks and thus sent to different workers.\r\n\r\n  To identify the distinct *fixture instances* that each test depends on, we need the following pieces of information for each test:\r\n\r\n  1. Test name.\r\n  2. For each fixture that the test depends on:\r\n      1. Fixture name.\r\n      2. Fixture scope and \"scope instance\".\r\n          * e.g. This fixture is module-scoped and this particular instance of the fixture is for module `test_a.py`.\r\n      3. Fixture parameter inputs, if the fixture is parameterized.\r\n          * e.g. We need to distinguish `fixture(request.param=1)` and `fixture(request.param=2)` as separate fixture instances.\r\n\r\n  Initially this information won't be used for anything. It will just be made available to the master. At this point, we'll be ready for the final phase.\r\n\r\n3. **Using the new information we have about tests, along with the new internal abstraction of a test chunk, we can now chunk tests up by the list of unique fixture instances they depend on.**\r\n\r\n  Tests that depend on the same instance of a fixture will now always be sent to the same worker. :tada:\r\n\r\n### Advantages over other solutions\r\n\r\nThis approach has two major advantages over other proposals.\r\n\r\n1. This approach adds no new configuration options or public APIs that users have to learn. Everyone using pytest automatically gets a better, arguably even more correct, distribution of their tests to workers without having to do any work.\r\n\r\n2. This approach promotes a *declarative* style of writing tests over an *imperative* one. The goal we should strive for is: **Capture your ideas correctly, and pytest will figure out the appropriate execution details.** In practice, this is probably not always feasible, and the user will want to exercise control in specific cases. But the closer we can stick to this goal the better the pytest user experience will be.\r\n\r\n\r\n## How this approach addresses common use cases\r\n\r\nThere are several use cases described in the [original issue requesting some way to control parallel execution](https://github.com/pytest-dev/pytest/issues/175). This is how the approach described here addresses those use cases:\r\n\r\n* Use case: \"I want all the tests in a module to go to the same worker.\"\r\n\r\n  Solution: Make all the tests in that module depend on the same module-scoped fixture.\r\n\r\n  If the tests don't need to depend on the same fixture, then why do they need to go to the same worker? (The most likely reason is that they are not correctly isolated.)\r\n\r\n* Use case: \"I want all the tests in a class to go the same worker.\"\r\n\r\n  Solution: The solution here is the same. If the tests need to go to the same worker, then they should depend on a common fixture.\r\n  \r\n  To cleanly address this case, we may need to implement the concept of a class-scoped fixture. (Fixtures can currently be scoped only by function, module, or session.) Otherwise, the tests can depend on a common, module-scoped fixture and achieve the same result.\r\n\r\n* Use case: \"I want all the tests in X to go to the same worker.\"\r\n\r\n  Solution: You know the drill. **If tests belong on the same worker, we are betting that there is an implied, shared dependency. Express that shared dependency as a fixture and pytest will take care of the rest for you.**\r\n\r\nCounter-proposal to #17.\r\nAddresses https://github.com/pytest-dev/pytest/issues/175.\r\nAdapted from [this comment](https://github.com/pytest-dev/pytest/issues/175#issuecomment-112200644) by @hpk42.\r\n"},"comment":{"url":"https://api.github.com/repos/pytest-dev/pytest-xdist/issues/comments/161468350","html_url":"https://github.com/pytest-dev/pytest-xdist/issues/18#issuecomment-161468350","issue_url":"https://api.github.com/repos/pytest-dev/pytest-xdist/issues/18","id":161468350,"user":{"login":"nicoddemus","id":1085180,"avatar_url":"https://avatars.githubusercontent.com/u/1085180?v=3","gravatar_id":"","url":"https://api.github.com/users/nicoddemus","html_url":"https://github.com/nicoddemus","followers_url":"https://api.github.com/users/nicoddemus/followers","following_url":"https://api.github.com/users/nicoddemus/following{/other_user}","gists_url":"https://api.github.com/users/nicoddemus/gists{/gist_id}","starred_url":"https://api.github.com/users/nicoddemus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nicoddemus/subscriptions","organizations_url":"https://api.github.com/users/nicoddemus/orgs","repos_url":"https://api.github.com/users/nicoddemus/repos","events_url":"https://api.github.com/users/nicoddemus/events{/privacy}","received_events_url":"https://api.github.com/users/nicoddemus/received_events","type":"User","site_admin":false},"created_at":"2015-12-02T23:40:52Z","updated_at":"2015-12-02T23:40:52Z","body":"@nchammas wow, congratulations on a very well written and thought out proposal! :smile: \r\n\r\nIt clearly states the problems we have today, the solution and perhaps most importantly the steps to achieve it. I will give it some more thought and post my comments later.\r\n\r\nThanks!"}}