arosu
arosu

Reputation: 65

Python unittest - how to chose the url on which the tests are executed?

I am somewhat of a beginner in python, i am currently writing a suite of test cases with selenium webdriver using unittest; i have also found a lot of useful answers here, but it's time a ask my first question, i have struggled a lot with this and cannot find a proper answer, so any help is greatly appreciated:

For short, i have a suite of multiple tests cases, and in each case the first step is always ".get('Some URL')"; i have written these test cases for a single environment, but i would like to be able to select the URL on which all tests will be executed. In the example below i called the "access_url" method with a specific environment, but i need to do this for all of my scenarios at once, is it possible to do this from where i execute the .py file (e.g. "python example.py")? or to pass it in the .run() method when i select what suite to run?

import HTMLTestRunner
from selenium import webdriver
import unittest

This is a custom class used to create the 'access_url' method

def MyClass(object):
    def __init__(self, driver):
        self.driver = driver

    def access_url(self, URL):
        if URL == 'environment 1':
            self.driver.get('https://www.google.com/')
        elif URL == 'environment 2':
            self.driver.get('https://example.com/')

In the classes i use to write test cases the first step is always 'access URL'

class TestScenario01(unittest.TestCase):

    def setUp(self):
        [...]

    def test_01_access(self):
        MyClass(self.driver).access_url(URL='environment 2')

    def test_02(self):
        [...]

    def test_03(self):
        [...]

In order to run the tests i place them all in a suite and use .run() on them

tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
test_suite = unittest.TestSuite([tc_scenario01])
HTMLReporterCustom.HTMLTestRunner().run(test_suite)

Finally, in order to execute the script i type the follwoing line in CMD: 'python example_file.py

As i mentioned above, all i want to do is to be able to somehow pass the URL one time to all test cases that call the "access_url()" method. Thanks!

Upvotes: 3

Views: 2103

Answers (1)

Navarasu
Navarasu

Reputation: 8489

You can maintain environment properties in separate config file.

config.py

DEFAULT_ENVIRONMENT='environment1'

URL = {
    'environment1': 'https://www.google.com/',
    'environment2': 'https://example.com/'
}

Your Class,

from package import config

def MyClass(object):
    def __init__(self, driver):
        self.driver = driver

    def access_url(self):
        self.driver.get(config.URL[config.DEFAULT_ENVIRONMENT])

Then test class will be as expected,

class TestScenario01(unittest.TestCase):

    def setUp(self):
        [...]

    def test_01_access(self):
        MyClass(self.driver).access_url()

    def test_02(self):
        [...]

    def test_03(self):
        [...]

While running test you can change,

main.py

from package import config

config.DEFAULT_ENVIRONMENT = 'enviroment2'
tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
test_suite = unittest.TestSuite([tc_scenario01])
HTMLReporterCustom.HTMLTestRunner().run(test_suite)

You can also pass the environment name while running python main.py.

main.py

if __name__ == '__main__':
    config.DEFAULT_ENVIRONMENT = sys.argv[1] if len(sys.argv) > 2 else 'dev'
tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
test_suite = unittest.TestSuite([tc_scenario01])
HTMLReporterCustom.HTMLTestRunner().run(test_suite)

Upvotes: 0

Related Questions