1

I am wondering if it is possible to use data generated from one test as parameter to another test. In my case I need to modify variable (this is list) and it will be great if I can use this list as param (run as many tests as list have)

Here is code (it is not working, but maybe You can give me some hints)

import pytest


class TestCheck:

    x = [1, 4]


    @classmethod
    def setup_class(self):
        print('here setup')

    @classmethod
    def teardown_class(self):
        print('here is finish')

    def test_5(self):
        self.x.append(6)
        assert 1 == 2

    @pytest.mark.parametrize("region", x)
    def test_6(self, region):
        assert region > 5, f"x: {self.x}"

Output:

FAILED sandbox_tests3.py::TestCheck::test_5 - assert 1 == 2
FAILED sandbox_tests3.py::TestCheck::test_6[1] - AssertionError: x: [1, 4, 6]
FAILED sandbox_tests3.py::TestCheck::test_6[4] - AssertionError: x: [1, 4, 6]

So it looks that in x there is good values, however in fixture new values are not visible.

I was also trying to use pytest_cases, but results are very similar.

Any help is appreciate

Bartek Z
  • 11
  • 2
  • You should create the input list to every test, it is not a good practice to have dependency between tests because they wouldn't be unitary test. – Fran Arenas Feb 17 '22 at 13:52
  • It is complicated:) I'm aware that this is not a good approach, but I believe that in my case it is the best approach. What do You mean by "input list"?. I need to run test_5 as first (before test_6), because of output from test 5 gives me arguments needed by test 6. As I've written I can read this values (x) in test_6, but I'd like to use it as parameter. – Bartek Z Feb 17 '22 at 20:48
  • Does this answer your question? [Chaining tests and passing an object from one test to another](https://stackoverflow.com/questions/49238725/chaining-tests-and-passing-an-object-from-one-test-to-another) – Devang Sanghani Feb 18 '22 at 05:29
  • @DevangSanghani, no it doesn't. I need to use results as parameters. – Bartek Z Feb 18 '22 at 07:24

0 Answers0