I'm using pytest to test my app.
pytest supports 2 approaches (that I'm aware of) of how to write tests:

In classes:


test_feature.py -> class TestFeature -> def test_feature_sanity


In functions:


test_feature.py -> def test_feature_sanity

Is the approach of grouping tests in a class needed? Is it allowed to backport unittest builtin module?
Which approach would you say is better and why?
Typically in unit testing, the object of our tests is a single function. That is, a single function gives rise to multiple tests. In reading through test code, it's useful to have tests for a single unit be grouped together in some way (which also allows us to e.g. run all tests for a specific function), so this leaves us with two options:

Put all tests for each function in a dedicated module
Put all tests for each function in a class

In the first approach we would still be interested in grouping all tests related to a source module (e.g. utils.py) in some way. Now, since we are already using modules to group tests for a function, this means that we should like to use a package to group tests for a source module.
The result is one source function maps to one test module, and one source module maps to one test package.
In the second approach, we would instead have one source function map to one test class (e.g. my_function() -> TestMyFunction), and one source module map to one test module (e.g. utils.py -> test_utils.py).
It depends on the situation, perhaps, but the second approach, i.e. a class of tests for each function you are testing, seems more clear to me. Additionally, if we are testing source classes/methods, then we could simply use an inheritance hierarchy of test classes, and still retain the one source module -> one test module mapping.
Finally, another benefit to either approach over just a flat file containing tests for multiple functions, is that with classes/modules already identifying which function is being tested, you can have better names for the actual tests, e.g. test_does_x and test_handles_y instead of test_my_function_does_x and test_my_function_handles_y.
Grouping tests in pytest: Classes vs plain functions