Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest test class calling class methods, Type error takes exactly 2 arguments (1 given)

I have a test class to test my methods but i have some issue with passing self, both of them are inside class and test class.

my method:

def get_all_links(self):
    """return all the links inside an html

    :return: list of links from an html page
    :rtype: list
    """
    return self.html.find_all('a')

my test case:

    @parameterized.expand(["http://www.google.com", "http://www.walla.com"])
    def test_get_all_links_known_links(self, known_link):
        """check get_all_links with a known link list

        :param known_link: lick to find
        :type known_link: str
        """
        html = Parser(open(os.path.normpath(os.path.join(self.root, "test.html"))))

        self.assertTrue(any([known_link in str(l) for l in html.get_all_links()]))

error:

E TypeError: test_get_all_links_known_links() takes exactly 2 arguments (1 given)

/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py:329: TypeError
...
like image 663
Kobi K Avatar asked May 27 '15 11:05

Kobi K


2 Answers

You really don't need to subclass unittest.TestCase here:

You can also "parametize" tests using pytest as well:

Example:

import pytest


from app.objects import Root  # Example


known_links = [
    "http://www.google.com",
     "http://www.walla.com"
]


@pytest.fixture()
def root(request):
    return Root()  # Root object


@pytest.mark.parametrize("known_links", known_links)
def test_get_all_links_known_links(root, known_link):
    html = Parser(
        open(os.path.normpath(os.path.join(root, "test.html")))
    )

    assert any([known_link in str(l) for l in html.get_all_links()])

See:

  • pytest: Fixtures: explicit, modular, scalable
  • pytest: Parametrizing tests
like image 179
James Mills Avatar answered Nov 02 '22 14:11

James Mills


You need to import parameterized before using the decorator:

from nose_parameterized import parameterized
like image 2
chown Avatar answered Nov 02 '22 13:11

chown