Skip to content
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

invoke setUpClass() many times under running multiple cpu mode #15

Closed
heipark opened this issue Nov 30, 2015 · 12 comments
Closed

invoke setUpClass() many times under running multiple cpu mode #15

heipark opened this issue Nov 30, 2015 · 12 comments
Labels

Comments

@heipark
Copy link

heipark commented Nov 30, 2015

os: win7
py:2.7 && pytest (2.8.2) && pytest-xdist (1.13.1)

import unittest

class SomeTest(unittest.TestCase):

    def test_unit1(self):
        pass

    def test_unit2(self):
        pass

    def test_unit3(self):
        pass

    def test_unit4(self):
        pass                        

    def test_unit4(self):
        pass         

    def test_unit5(self):
        pass     

    @classmethod
    def setUpClass(cls):
        with open('c:/abc.txt', 'a') as f :
            f.write('######\n')

command:

py.test testme.py   -v -n 3

content of c:/abc.txt:

######
######
######

xdist can not guarantee setUpClass() only run once ?

@spinus
Copy link
Member

spinus commented Nov 30, 2015

It's one per process. So how it works is, xdist takes all the tests and split them into 3 test sets, and set up function is executed for each set.
It's like that for example because tests can be executed on remote hosts and than you want to run set up.

In you case I think this is badly written test. I think you should use https://docs.python.org/2/library/tempfile.html to create test file/directories and remove them at tear_down, or better, there are some fixtures which helps with that (https://pytest.org/latest/tmpdir.html)

@RonnyPfannschmidt
Copy link
Member

that's working as designed

@heipark
Copy link
Author

heipark commented Dec 1, 2015

hello spinus

in my scenario, I want to insert data to database in setUpClass() in every test case. Repeatedly calling the method will lead to data chaos :(
How to solve this problem?

thx

@heipark
Copy link
Author

heipark commented Dec 1, 2015

i found this topic, maybe i can try the solution of @piwai by using ClassLoadScheduling

"way to control how pytest-xdist runs tests in parallel? #175"
pytest-dev/pytest#175

@heipark
Copy link
Author

heipark commented Dec 1, 2015

i fork xdist and merge @piwai code,and fix problem,now it works.

https://github.com/heipark/pytest-xdist

@spinus
Copy link
Member

spinus commented Dec 1, 2015

@heipark why not just isolate the tests? You can create database in setup method (with worker number in the name)

@RonnyPfannschmidt
Copy link
Member

that fork looks interesting

@RonnyPfannschmidt
Copy link
Member

its solving a completely different problem that incidentially solves the problem @heipark has due to distribution strategies

@nicoddemus
Copy link
Member

I have been working on the background on writing up a more generic proposal on controlling how xdist distributes the test across slaves in --dist=load, checkout #17.

@heipark
Copy link
Author

heipark commented Dec 2, 2015

@spinus how to get worker name (subprocess name ?) in setUp() and test method?

thx

@namgivu
Copy link

namgivu commented Dec 23, 2018

@spinus how to get worker name (subprocess name ?) in setUp() and test method?

thx

Same with @heipark, how to get worker number/name?

@nicoddemus
Copy link
Member

You can use an environment variable for that, I forget the name right now but it is in the readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants