[pytest-dev] ANN: nose2pytest 1.0

Thomas De Schampheleire patrickdepinguin at gmail.com
Thu Mar 31 04:23:00 EDT 2016


Hi Oliver,

On Thu, Mar 31, 2016 at 6:04 AM, oliver <oliver.schoenborn at gmail.com> wrote:
> Announcing a project hosted on GitHub called nose2pytest
> (https://github.com/schollii/nose2pytest). This project helps migrate a test
> suite that was written for Nose to work with pure pytest. It converts
> nose.tools.assert_ functions into raw assert statements so you can better
> (IMO) leverage pytest assertion introspection. It may even decrease test
> suite dependencies by 1; after nose2test has been run on a test suite, it is
> no longer needed.
>
> The nose2pytest script has already successfully converted approximately 5000
> assertions, so this is v1.0. However, there are many ways to harness Nose
> functionality so I'm really curious to see if nose2test can be useful to
> others.
>
> I'm sure there is lots of room for improvement. Any feedback or
> contributions would be much appreciated.
>

Being precisely in the process of migrating a nose-based application
(Kallithea) to pytest, this sounds very interesting.
With the help of a pytest-month initiative last April, we already
became able of using pytest as the test runner, but the actual test
cases had not been converted until recently. Now I am doing just that.

However, our tests are not using assert methods of nose but of
unittest, e.g. self.assertEqual, self.assertRaises, ...
As a temporary measure, I implemented wrapper functions in our pytest
base test class:

    # transitional implementations of unittest.TestCase asserts
    # Users of these should be converted to pytest's single 'assert' call
    def assertEqual(self, first, second, msg=None):
        assert first == second
    def assertNotEqual(self, first, second, msg=None):
        assert first != second
    def assertTrue(self, expr, msg=None):
        assert bool(expr) is True
    def assertFalse(self, expr, msg=None):
        assert bool(expr) is False
    def assertIn(self, first, second, msg=None):
        assert first in second
    def assertNotIn(self, first, second, msg=None):
        assert first not in second
    def assertSetEqual(self, first, second, msg=None):
        assert first == second
    def assertListEqual(self, first, second, msg=None):
        assert first == second
    def assertDictEqual(self, first, second, msg=None):
        assert first == second
    def assertRaises(self, exception, method):
        with pytest.raises(exception):
            method()

I had a very brief look at your nose2pytest code, and at first sight
it doesn't look complicated to support a unittest2pytest conversion
too? What do you think of that?

Aside from the assert functions, another type of conversion I had to
make was from unittest-style setUp/tearDown functions to pytest-style:

setUp(self) --> setup_method(self, method)
tearDown(self) --> teardown_method(self, method)
setUpClass(cls) --> setup_class(cls)
__init__(self, ...) --> setup_class(cls)
...

While your README mentions that some replacement can be done with
simple search/replace, I think it would be most useful if even such
things were handled by nose2pytest, so that it becomes a one-stop
solution.

Looking forward to your feedback,
Thomas


More information about the pytest-dev mailing list