vendredi 2 octobre 2015

Run python script from terminal adding parameters

I am working with Python 2.7. I create the following script:

import unittest
from tests.selenium_test_base import SeleniumTestBase
import sys

class TestPrintArg(SeleniumTestBase):

    def test_print_arg(self):
        print "BEGIN: "+str(len(sys.argv))
        for elements in sys.argv:
            print elements
        print "END:"

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

I executed from terminal using: python test_print_arg.py

But if I executed with arguments: python test_print_arg.py arg1

I am getting the following issue:

 Traceback (most recent call last):
  File "test_verify_page.py", line 19, in <module>
    unittest.main()
  File "C:\Python27\lib\unittest\main.py", line 94, in __init__
    self.parseArgs(argv)
  File "C:\Python27\lib\unittest\main.py", line 149, in parseArgs
    self.createTests()

C:\stash\sxd_qa\tests\log_in>python test_verify_page.py arg1
BEGIN: 2
test_verify_page.py
arg1
END:
Traceback (most recent call last):
  File "test_verify_page.py", line 19, in <module>
    unittest.main()
  File "C:\Python27\lib\unittest\main.py", line 94, in __init__
    self.parseArgs(argv)
  File "C:\Python27\lib\unittest\main.py", line 149, in parseArgs
    self.createTests()
  File "C:\Python27\lib\unittest\main.py", line 158, in createTests
    self.module)
  File "C:\Python27\lib\unittest\loader.py", line 130, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Python27\lib\unittest\loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'arg1'

Could you help me? The browser is not launched if I want to use parameters.

Aucun commentaire:

Enregistrer un commentaire