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.

Convert some `unittest` tests to `pytest` style

See original GitHub issue

Some of our tests follow the unittest structure of creating tests. While these are fine, we want to migrate our tests to be in the more functional style of pytest.

For example, test_classification which tests the classification pipeline adopts the unittest style of class structured tests in which you must understand the whole setup to often understand one test.

In contrast, a more functional pytest looks like test_estimators.py::test_fit_performs_dataset_compression. Here we can define the test entirely through parameters, specify multiple parameter combinations, document which ones we expect to fail and give a reason for it. Because the test is completely defined through parameters, we can document each parameter and make the test almost completely-transparent by just looking at that one test.

We would appreciate any contributors on this side who want to get more familiar or are already familiar with testing. Some familiarity or willingness to learn pytest’s @parametrize and @fixture features is a must. Please see the contribution guide if you’d like to get started!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
eddiebergmancommented, Aug 24, 2022

Hi @shantam-8,

So I took at test_metrics and I have a few pointers.

  • Thankfully the tests look very straightt forward, they create some dummy data, they create a scorer and then they make sure it gives the correct output.
  • We want to do the following migration:
# From
import unittest

class TestScorer(unittest.TestCase):

    def one(self):
        ...

# To
def test_one() -> None:
    ...
  • You can use the following replacements:
# From
self.assertAlmostEqual(a, b)
self.assertTrue(t)

# To
from pytest import approx
assert a == approx(b)
assert t is True
def test_sign_flip(...) -> None:
    """
    Expects
    -------
    * Flipping greater_is_better for r2_score should result in flipped signs of
    its output. 
    """
  • If there’s any test that seems like it’s actually testing 2 things or many things at once, feel free to split them up into seperate tests, use your own judgement and we’ll just discuss it during the PR 😃

Please feel free to make any other changes that make sense in your judgement, as long as the core thing being tested is still being tested! You should also check out the CONTRIBUTING.md, especially the section on testing to understand how you can run only the tests you care about. Using pytest test/test_metric should be enough but there’s more info in the guide if you need.

You can continue asking things here or make an initial PR and we can discuss further there at any point!

Thanks for thinking to contribute 😃

Best, Eddie

0reactions
shantam-8commented, Aug 24, 2022

Really thanks a lot for your help! I shall open a PR asap.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use unittest-based tests with pytest
To run an existing unittest -style test suite using pytest , type: pytest tests. pytest will automatically collect unittest.TestCase subclasses and their ...
Read more >
How can I convert Python unittests to py.test when having ...
I do have a set of unit tests written using the Python's unittest module. They are using the setUpModule() function to load a...
Read more >
Moving from `unittest` to `pytest` - JJ's World
Looking back at the tests now, I noticed that the Character/Castle testset is not very tidy so at the end I will simply...
Read more >
Effective Python Testing With Pytest
In this tutorial, you'll learn how to take your testing to the next level with pytest. You'll cover intermediate and advanced pytest ......
Read more >
unittest — Unit testing framework — Python 3.11.1 ...
Some users will find that they have existing test code that they would like to run from unittest , without converting every old...
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