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

Merged
merged 3 commits into from
Aug 20, 2025
Merged

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

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Looking good! Just a couple small suggestions.

@bwhitt7
Copy link
Contributor Author

bwhitt7 commented Aug 20, 2025

@ngoldbaum Made the changes! Found a few other spots where the code didn't lineup and tried fixing it.

@ngoldbaum
Copy link
Member

Thanks @bwhitt7!

@ngoldbaum ngoldbaum merged commit 3545479 into numpy:main Aug 20, 2025
77 checks passed
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