watch-this
watch-this

Reputation: 1

TypeError: TestViews.test_fails() missing 1 required positional argument: 'param'

I'm trying to run tests on a Django app using selenium + pytest which according to the docs, below should work.

import pytest
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.webdriver import Chrome


class TestViews(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.driver = Chrome()

    @classmethod
    def tearDownClass(cls):
        if hasattr(cls, 'driver'):
            cls.driver.quit()
            super().tearDownClass()

    @pytest.mark.parametrize('param', ['param1', 'param2'])
    def test_fails(self, param):
        pass

However, I get:

Creating test database for alias 'default'...
Found 1 test(s).
System check identified no issues (0 silenced).

Error
TypeError: TestViews.test_fails() missing 1 required positional argument: 'param'
  
Destroying test database for alias 'default'...

Upvotes: 1

Views: 291

Answers (1)

Michael Mintz
Michael Mintz

Reputation: 15556

StaticLiveServerTestCase inherits unittest.TestCase, which means that you cannot use @pytest.mark.parametrize to expand your test. Fortunately, you can use parameterized instead.

See Does pytest parametrized test work with unittest class based tests? for the related question.

And see https://stackoverflow.com/a/52062473/7058266 for the workaround.

Here's what your code would look like after swapping out @pytest.mark.parametrize with parameterized:

from parameterized import parameterized
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.webdriver import Chrome

class TestViews(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.driver = Chrome()

    @classmethod
    def tearDownClass(cls):
        if hasattr(cls, 'driver'):
            cls.driver.quit()
            super().tearDownClass()

    @parameterized.expand([
        ['param1'],
        ['param2'],
    ])
    def test_fails(self, param):
        pass

Upvotes: 1

Related Questions