1

I've started using unitest infrastructure that based on pytest for adding additional test class (see below TestMyStuff)

It seems that if I add the fixtures xi and yi as inputs of method setup_method it refer to them as functions from fixtures.py, but adding them to each of the test functions, they get their return value of classX and classY respectively rather then the function value.

Both test methods share identical code block which require xi and yi, and also an additional per-test unique code. I wish to take the common code and add it to test_method but for that I need that xi and yi as instances of classX and classY which the fixtures method returns, and not the methods themselves from fixtures.py

In brief, is there any way to share some pre-built fixtures between all class functions (both the test methods and the prolog method) ?

test_xy.py

from fixtures import *

class TestMyStuff
    def setup_method(self, test_method, xi, yi):
        # here xi and yi are functions :-(
        self.x = classX.makeX()
        self.y = classY.makeY()


    def test_X(self, xi, yi):
        # here xi and yi are instances of classX and classY
        # do shared stuff with xi and yi
        # do unique stuff with xi and yi

    def test_Y(self, xi,yi):
        # here xi and yi are instances of classX and classY
        # do shared stuff with xi and yi
        # do other stuff with xi and yi

fixtures.py

from classY import classY
from classX import classX


@pytest.fixture()
def xi(request):
    ...
    return classX(request.var1, request.var2, request.var3)
    #classX is implemented on different file called classX.py

@pytest.fixture()
def yi(request, xi)
    ...
    return classY(request.var1, xi) 
Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • Are you bound to the xUnit's `setup_method`? You can replace it with an autouse fixture that does effectively the same. Check out [my recent answer to a similar question](https://stackoverflow.com/a/56823587/2650249) for a code example. – hoefling Jul 02 '19 at 08:14
  • Hi and thanks for you reply. I've read your answer, and the internal fixtures are called for each test which is great, but what if I want the internal fixtures to operate just once for multiple test running. for example, if I run all tests in class `c:\Python27\Scripts\pytest.exe -k TestMyStuff`. – Zohar81 Jul 02 '19 at 08:28
  • Just change the fixture scope to `class` so it executes once per class; I have an example for that in the answer. Search for text _One-time setup/teardown per class_. – hoefling Jul 02 '19 at 08:34
  • If you mean the fixtures `xi`/`yi`, you can e.g. even adjust their scope to `session` and they will execute once for the whole test run; values will be calculated once when first requested and cached for subsequent requests. – hoefling Jul 02 '19 at 08:41
  • But if I use `xi` and `yi` with scope `session` from the internal fixtures from scope `class` I get : `You tried to access the 'class' scoped fixture 'xi' with a 'session' scoped request object, involved factories` – Zohar81 Jul 02 '19 at 08:44
  • Can you update your question with a failing example? I can't reproduce it locally. – hoefling Jul 02 '19 at 08:49
  • It actually works now. I hadn't switched all my fixtures to the correct scope and therefore it failed before. But one more thing, can I control the scope using some sort of input parameters, since some other test classes are using the default scope for `xi` and `yi`. thanks ! – Zohar81 Jul 02 '19 at 10:43
  • You can't control the scope, and in fact you shouldn't. For example, a session scoped fixture changing the scope to class would suddenly redefine its return value, breaking the tests that expect it to be session-scoped. Scope sets are inclusive though, so test classes can use session-scoped fixtures very well. If you describe your problem, I'm sure there's a solution for it using fixtures only. – hoefling Jul 02 '19 at 16:42

1 Answers1

0

From the pytest documentation:

You can mix both fixture mechanisms in the same file but test methods of unittest.TestCase subclasses cannot receive fixture arguments.

https://docs.pytest.org/en/latest/xunit_setup.html

I do not think you can build things as elegantly as you would like.

Grant
  • 1
  • 3