Skip to content

TST: Replace xunit setup with methods #29596

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bwhitt7
Copy link
Contributor

@bwhitt7 bwhitt7 commented Aug 19, 2025

This PR is related to #29552, with the goal of making NumPy's testing suite more thread safe. This PR is "testing the waters" with a test file that had a lot of simple xunit setup methods. All setup methods were replaced with methods that are manually called in each test. This makes the tests more thread safe, since xunit setup methods work strangely with threads. An example of a change:

# TestComparisons
def setup_method(self): # setup method to be replaced
    self.A = np.array([['abc', 'abcc', '123'],
                       ['789', 'abc', 'xyz']]).view(np.char.chararray)
    self.B = np.array([['efg', 'efg', '123  '],
                       ['051', 'efgg', 'tuv']]).view(np.char.chararray)

    def test_not_equal(self):
        assert_array_equal((self.A != self.B), # calling self to access variables
                           [[True, True, False], [True, True, True]])

to

# TestComparisons
def _create_array_a(self): # "creation" methods used instead
    return np.array([['abc', 'abcc', '123'],
                        ['789', 'abc', 'xyz']]).view(np.char.chararray)

def _create_array_b(self):
    return np.array([['efg', 'efg', '123  '],
                        ['051', 'efgg', 'tuv']]).view(np.char.chararray)

def test_not_equal(self):
    (A, B) = (self._create_array_a(), self._create_array_b()) # calling creation methods to set up variables
    assert_array_equal((A != B),
                       [[True, True, False], [True, True, True]])

With this PR, you should be able to call the tests from this file with pytest-run-parallel and have all tests successfully run in parallel threads (and also run normally without pytest-run-parallel).

Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR.

These changes caused several array definitions to not be aligned anymore, could you fix those?

@bwhitt7
Copy link
Contributor Author

bwhitt7 commented Aug 19, 2025

Hopefully this push should address your suggestions @ngoldbaum @jorenham

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

Successfully merging this pull request may close these issues.

3 participants