Skip to content

How to pass a variable from a test to another test #3403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks
aMillionTears opened this issue Apr 16, 2018 · 6 comments
Closed
4 tasks

How to pass a variable from a test to another test #3403

aMillionTears opened this issue Apr 16, 2018 · 6 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@aMillionTears
Copy link

aMillionTears commented Apr 16, 2018

I write a test class, it has two test methods. What I want to do is, at the first test I'd like to get some result and assign to a variable; and at the second test I want to do some test depend on this variable. What's the best way to do this? Currently, I use a "global", and I know I can put these two tests into a same method. But I look forward a better solution.

Thanks for submitting an issue!

Here's a quick checklist in what to include:

  • Include a detailed description of the bug or suggestion
  • pip list of the virtual environment you are using
  • pytest and operating system versions
  • Minimal example if possible
@pytestbot pytestbot added the type: question general question, might be closed after 2 weeks of inactivity label Apr 16, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #2472 (Passing global variable from test function), #3118 (How can I use global variables through tests?), #1694 (Add possibility to pass parameter to the fixture from another fixture), #288 (Pass test result to finalizer for cleaning up debug data), and #153 (test intents).

@rachel1792
Copy link

Your first 'test' doesn't sound like a test. Does it include any assert statements?

Instead, you can make it a helper method to compute the value you need for your second test, and call that helper method in your second test. Just take of the test prefix or postfix from its name and pytest won't treat it as a test. Hope that helps!

@aMillionTears
Copy link
Author

aMillionTears commented Apr 18, 2018

@rachel1792
My first 'test' is a test. The scenario likes this:
First, I want to test 'Create an item', and return the item's Id.
Then, I want to test 'Edit this item', and I should give this item_id to the test telling it which item to edit.
So, I need to collect first test's data, then give the data to next test.

@nicoddemus
Copy link
Member

@zzyGit that's not possible without some ugly hack, but unittests are supposed to be idempotent and independent from each other. You should be able to run tests in any other.

But @rachel1792's advice is sound, why don't you create a fixture or method which creates the id for you? If the fixture fails for any reason, you will still get an error.

Another strategy is using something like:

@pytest.fixture
def item():
    return Item()

def test_item_created(item):
    # ensure item was created successfully
    assert item.value == 'something expected'

def test_edit_item(item):
    # ensure item is edited successfully
    item.edit()

@nicoddemus
Copy link
Member

Closing as this has not seen activity in awhile.

@alexandru-cleverbase
Copy link

alexandru-cleverbase commented Aug 30, 2019

  1. Create a class in conftest.py and import it in your test module. To protect yourself from errors caused by failed testcases that were suppose to add values in the class, assign None to all the known values.
    conftest.py:
class ValueStorage:
    value1 = None
    value2 = None

test_mytest.py:

from conftest import ValueStorage

def test_one():
    ValueStorage.value1 = "anything you want here"
def test_two():
    assert ValueStorage.value1 == "anything you want here"
    ValueStorage.value2 = ValueStorage.value1

class TestClass:
    def test_three(self):
        assert ValueStorage.value1 == ValueStorage.value2

You can pass dictionaries and lists and anything you feel like. If you require persistence (want the values to be kept from one run to another you can write them in a json file or use the cache option)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

5 participants