1

I have multiple tests inside one test case but I am noticing that it is only running the first test

import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait

class Test(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "http://www.example.com/"
    def test_Google(self):
        driver = self.driver
        driver.implicitly_wait(10)
        driver.get(self.base_url)
    def fill_contact(self):
        driver.find_element_by_xpath('//a[contains(.,"Contact")]').click()
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@type="submit"][@value="Send"]').click()

    # def tearDown(self):
    #     self.driver.quit()

if __name__ == "__main__":
    unittest.main()

Whenever I run this it only runs

def test_Google(self)

and nothing after that. am I doing something wrong?

programiss
  • 255
  • 1
  • 5
  • 16

2 Answers2

3

Methods must begin with 'test' to be automatically run.

Per the docs:

A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests. (my emphasis)


The TestLoader is responsible for loading test and returning them wrapped in a TestSuite. It uses this method to identify tests:

class TestLoader(object):
    testMethodPrefix = 'test'
    def getTestCaseNames(self, testCaseClass):
        """Return a sorted sequence of method names found within testCaseClass
        """
        def isTestMethod(attrname, testCaseClass=testCaseClass,
                         prefix=self.testMethodPrefix):
            return attrname.startswith(prefix) and \
                hasattr(getattr(testCaseClass, attrname), '__call__')
        testFnNames = filter(isTestMethod, dir(testCaseClass))
        ...

Thus, the attrname.startswith(prefix) checks if the method name begins with 'test'.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

As an alternative to what @unubtu noted:

you can use a nose test runner and mark a method with @istest decorator:

from nose.tools import istest

class Test(unittest.TestCase):
    ...

    @istest
    def fill_contact(self):
        driver.find_element_by_xpath('//a[contains(.,"Contact")]').click()
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@type="submit"][@value="Send"]').click()

Besides, here is a pretty good overview of the unittest test discovery:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195