8

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
...
Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • 2
    Actually; are you using [nose](https://nose.readthedocs.org/en/latest/) or pytest? You question title implies [pytest](https://pytest.org) -- Even your tag includes ``pytest``. -- Which is it? – James Mills May 27 '15 at 12:05
  • @James Mills I am using pytest but trying to use nose parameterized with it. – Kobi K May 27 '15 at 12:31
  • 2
    No; use pytest's parameterization and fixtures :) Please! Don't over complicated things be mixing two test frameworks! – James Mills May 27 '15 at 12:33

3 Answers3

4

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:

James Mills
  • 18,669
  • 3
  • 49
  • 62
2

You need to import parameterized before using the decorator:

from nose_parameterized import parameterized
chown
  • 51,908
  • 16
  • 134
  • 170
  • Oh haha I didn't notice the OP was using node and not pytest :) Whoops! Hmm actually I'm not sure now :) – James Mills May 27 '15 at 12:04
  • Yea, although, he could actually be using both nose and unittest. Check out these examples: https://github.com/wolever/nose-parameterized – chown May 27 '15 at 12:12
  • Oh well *shrug*; the OP hasn't responded ot our comments so let's call it *two possible solutions*) :) – James Mills May 27 '15 at 12:23
  • @chown i am importing parameterized, but no i see the point, cant py.test use nose_parameterized? – Kobi K May 27 '15 at 12:26
  • @KobiK I'm not positive if it works with py.test or not. I've only seen it done with unittest. Check out that link above in my previous comment. It gives lots of usage examples for nose_parameterizes with unittest. – chown May 27 '15 at 12:28
  • I've tried it, same error i'm using it inside a class probably some stupid error. – Kobi K May 27 '15 at 12:32
0

add self. in front of html.get_all_links

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

Python must be able to find the method in the namespace. Through self (i.e. your object instance) it can find all entries in the instance namespace and all entries in its class namespace, etc. See here for info regarding classes/methods/attributes/namespaces. Also see my answer to a similar problem here

Community
  • 1
  • 1
Pynchia
  • 10,996
  • 5
  • 34
  • 43