Skip to content

TST: Converting to Pytest Idiom #15990

Closed
Closed
@gfyoung

Description

@gfyoung

Our testing framework is a literal hodgepodge of different 3rd-party framework standards (i.e. nosetest, pytest, unittest) that needs to be cleaned up.

A couple of points are clear:

  • Use assert instead of self.assert*...
  • When possible, replace for loops with pytest.mark.parametrize
  • All new test classes should inherit from object instead of tm.TestCase

However, we also have this massive testing module helper testing.py that is populated with tons of useful comparison methods as well as wrappers around basic assert methods that are used inconsistently throughout the code-base. Thus, the question is whether or not we want to keep using those OR remove them entirely and replace them with basic assert statements.

The advantage with using tm.assert statements is that they come directly with error messages instead of an empty AssertionError, which is what I don't like about raw assert statements that are considered more idiomatic with pytest.

Thus, it's a question of greater clarity (i.e. our error messages will be more useful by default when we get test failures) vs. idiomatic (raw assert with no wrapping is the idiomatic way to go but risks presenting less helpful failure messages).

Another point I wanted to bring up is with regards to the introduction of decorators in #15952 to capture stdout and stderr. pytest has fixtures to capture those, but incorporating it into the code-base is very difficult due to our heavy reliance still on unittest.TestCase, which causes incorporation to fail. The question is: do we eventually want to use the pytest fixture or continue using the decorator?

Thoughts?


From the discussion that follows, here are the functions that should be removed and replaced:

  • self (tm).assertRaises --> pytest.raises
  • (NO ACTION) self (tm).assertRaisesRegexp -->
with pytest.raises(cls) as exc_info:
   ...
exc_info.match(regex)

Per discussion in #16089 (comment), we are leaving as is.

  • self.assert* (examples follow)
    • self.assertIs
    • self.assertEqual
    • ...and many others
  • self.assert_* (examples follow)
    • self.assert_numpy_array_equal
    • self.assert_series_equal
    • self.assert_frame_equal
    • self.assert_index_equal
    • ...and many others
  • tm.assert_equal
  • tm.assertIs(Not)
  • tm.assert(Not)In
  • tm.assertIs(Not)None
  • tm.assert(Not)IsInstance

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsTestingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions