0

Is there any proper way to using getting url in cmd as argument along testcases.py file?

I am running below commmand in cmd to run test cases of python file: testcases.py "any url"

testcases.py have coding:

class JSAlertCheck(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome("E:\chromedriver.exe")
        self.url = sys.argv[1]

    def test_Case1(self):
        driver = self.driver

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

if __name__ == "__main__":
    unittest.main(sys.argv[1])
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Usman_Ahmed
  • 67
  • 1
  • 9
  • 1
    https://stackoverflow.com/questions/1842168/python-unit-test-pass-command-line-arguments-to-setup-of-unittest-testcase – Yash Pokar Jun 06 '18 at 09:17
  • 1
    Possible duplicate of [Python, unit test - Pass command line arguments to setUp of unittest.TestCase](https://stackoverflow.com/questions/1842168/python-unit-test-pass-command-line-arguments-to-setup-of-unittest-testcase) – Ofer Sadan Jun 06 '18 at 09:21

1 Answers1

0

As per the discussion Python unittest passing arguments the Python Pundits seems to convey that:

Unit tests should be stand-alone which will have no dependencies outside of their setUp() and tearDown() methods. This is to make sure that each tests has minimal side-effects and reactions to the other test. Passing in a parameter defeats this property of unittest and thus makes them sort of invalid. Using a Test Configuration would have been the easiest way and more appropiate as a unittest should never rely on foreign data to perform the test.

If you still want to do so, here is one of the working solution:

  • Code Block:

    from selenium import webdriver
    import unittest
    import sys
    
    
    class MyTest(unittest.TestCase):
    
        URL = "foo"
    
        def setUp(self):
            self.driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
            driver = self.driver
            driver.get(self.URL)
    
        def test_Case1(self):
            driver = self.driver
            print(driver.title)
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == "__main__":
        if len(sys.argv) > 1:
            MyTest.URL = sys.argv.pop()
        unittest.main()
    
  • CLI Command:

    python unittest_cmdline_urlASarguments.py http://www.python.org
    
  • Output:

    C:\Users\AtechM_03\LearnAutmation\PythonProject\readthedocs>python unittest_cmdline_urlASarguments.py http://www.python.org
    [4448:5632:0606/205445.017:ERROR:install_util.cc(589)] Unable to create registry key HKLM\SOFTWARE\Policies\Google\Chrome for reading result=2
    
    DevTools listening on ws://127.0.0.1:5634/devtools/browser/40cc6c16-1e52-4f49-a54f-08fac3ff7abc
    Welcome to Python.org
    .
    ----------------------------------------------------------------------
    Ran 1 test in 9.534s
    
    OK
    
    C:\Users\AtechM_03\LearnAutmation\PythonProject\readthedocs>
    
  • Commandline Snapshot:

unittest_cli_url

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352