Py.test: parametrize test cases from classes

If you subclass from unittest.TestCase, your test methods cannot have additional arguments. If you simply subclass from object, it will work (though you’ll have to use regular assert statements instead of the TestCase.assertEqual methods.

import unittest

import pytest

class TestCase(object):

    @pytest.mark.parametrize("test_input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
    ])
    def test_1(self, a, b):
        assert eval(a) == b

At that point though, it kind of begs the question why you’re using classes instead of just defining functions, since the test will essentially be the same, but require less overall boilerplate and code.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)