Forums

Selenium UnitTest.TestCase passing custom arguments

When we use UnitTest framework for automation using python,we are facing a problem while passed the input to the derived class, How do we pass args to the actual TestCase class we have created and how to call that function.

I'm not sure I understand the question. Can you provide more information about exactly what you're trying to achieve, and what you're currently doing? Maybe add a few bits of example code?

Harry, Thanks for checking this. In the below code(This is a simple test for login) ,i want to pass the custom user name and password .Here username and password (Ron and Password) is hard coded now.I want to pass it as input while calling the test cases.

import unittest,selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class Login(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url="http://webaddr"
    def test_search_in_python_org(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.implicitly_wait(10)
        username = driver.find_element_by_id("username")
        password = driver.find_element_by_id("password")
        username.send_keys("Ron")
        password.send_keys("password")
        driver.find_element_by_id("loginbtn").click()

    def tearDown(self):
        self.driver.close()

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

[edited by admin: code formatting]

When you say "as input", do you mean on the command line? Or do you want the test case to prompt the person who runs it for a username and password on the console? Or something else?

I mean passing as commandline... When i call unittest.main() i should pass all my required arguments.(my inputs)

I see. So, if you do a quick google search, you'll find a stackoverflow post (or several) asking the same question, and the one with the most votes has an answer saying "don't do that"

I tend to agree. If you want to test logging in as several different users, put them in a list, and iterate over them with a for loop. If you want to set up a specific user for your tests, use a test fixture, and use a subprocess.call with python manage.py loaddata.

But if you really, really want to pass arguments from the command line, you'll have to use some kind of hack. Like the dreaded global variables for example:

import sys
import unittest

class MyTest(unittest.TestCase):
    USERNAME = "Ron"
    PASSWORD = "password"

    def test_search_in_python_org(self):
        print 'username', self.USERNAME
        print 'password', self.PASSWORD


if __name__ == "__main__":
    if len(sys.argv) > 1:
        MyTest.PASSWORD = sys.argv.pop()
        MyTest.USERNAME = sys.argv.pop()
    unittest.main()

Ah yes, the other thing you might want to look into is py.test. All the cool kids are saying it's the best python testing framework these days, and it lets you parametrize tests:

Thanks a lot for the global variable solution harry.It helped!!