question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TST: Converting to Pytest Idiom

See original GitHub issue

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).

<strike>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?</strike>

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
jorisvandenbosschecommented, Apr 13, 2017

I don’t think we need to change any of the asserts in util/testing.py. They’re already doing the right thing.

I think @gfyoung meant the assert_equal, assertIsInstance, … wrappers we have in pandas.util.testing. And for those, yes, I think we should clean that up and remove those.

1reaction
jrebackcommented, Apr 13, 2017

yes the only things would change are

  • assert_equal (this is generally for scalar / lists /dicts)
  • self.assertEqual
  • self.assertTrue/False
  • assertDictEqual
  • assertIs
  • assertIsInstance

and use pytest.raises

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting asserts with unitest2pytest | pytest Quick Start Guide
It converts all self.assert* method calls to plain asserts, and also converts self.assertRaises calls to the appropriate pytest idiom.
Read more >
How to go about converting a test-suite from unittest to pytest?
There seems to be a conversion tool, though I haven't tested it. Generally, I would do this incrementally - if you want to...
Read more >
Usage and Invocations — pytest documentation
You can invoke testing through the Python interpreter from the command line: python -m pytest [...] This is almost equivalent to invoking the...
Read more >
Welcome to Pytest-BDD's documentation! — Pytest-BDD 6.1.1 ...
An example test for a blog hosting software could look like this. ... You can customize how tags are converted to pytest marks...
Read more >
Effective Python Testing With Pytest
pytest fixtures are a way of providing data, test doubles, or state setup to your tests. Fixtures are functions that can return a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found