1

I'm trying to figure out how to send a command line argument into a string, and I keep getting an error.

Here is my python code with the command argument

import sys
class LoginTest(unittest.TestCase):
    def setUp(self):
        buildURL = sys.argv[1]
        self.driver = webdriver.Chrome()
        self.driver.get("https://" + buildURL + "test.com")

Here is the command line argument I send.

python test.py build190 

And lastly the error I keep getting.

AttributeError: 'module' object has no attribute 'build190'

The full error message I receive

Traceback (most recent call last):
  File "Test.py", line 60, in <module>
    unittest.main()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'build190'
Cal Corbin
  • 190
  • 15
  • Tried using `buildURL=sys.argv.pop()` or `buildURL=sys.argv[1:]`? – Rahul Feb 10 '18 at 23:01
  • Please post the value of the buildURL variable after you assign it a value. Also - does this error happen on the assignment line or when you try to use it at self.driver.get(....) line? – Lee James Feb 10 '18 at 23:04
  • @LeeJames The error is well explained in the answer provided. It is the nature of how unittest runs. It is ultimately trying to use/discover the passed argument as a module, hence why that exception is raised. – idjaw Feb 10 '18 at 23:28
  • @idjaw when i posted my comment, there was no error information in the question ;) – Lee James Feb 10 '18 at 23:33
  • @LeeJames I was referring to the answer provided below. I see my comment did not express that properly. – idjaw Feb 10 '18 at 23:34
  • @idjaw - and again - when I posted my comment, there was no answer below :D – Lee James Feb 10 '18 at 23:37
  • @LeeJames OK. Sorry for sharing information with you then. ;) :) :D =D – idjaw Feb 10 '18 at 23:41

1 Answers1

3

You are trying to run unittest and how it loads is interfering with your intention to pass argument to a script.

Before we go further, unit tests should run as standalone and passing parameters to them is not a best practice. Here you can read reasons why not to do it.

Anyway, try this snippet. It should work.

import sys
import unittest

class LoginTest(unittest.TestCase):
    buildURL = ""

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://" + buildURL + "test.com")

if __name__ == "__main__":
    LoginTest.buildURL = sys.argv[1]
    unittest.main()

If you decide to go this path (passing parameter to a unit test), you should add some checking if there is any argument at all, and if you want to run with buildURL that is not set.

Ilija
  • 1,556
  • 1
  • 9
  • 12
  • 2
    Good break down. Just to add a minor detail about the exact nature of the error: Ultimately, the *exact* thing that is going on with your error, is that throughout the execution of `main()` and initialization of the unit test, it eventually tries to actually discover/load that argument as a module it expects to be there. For the OP, if you're interested, use your debugger to check out what is going on when main is called, or use `pdb` to step through the code through your command line to see when it breaks. It will make sense when you see it for yourself. – idjaw Feb 10 '18 at 23:26
  • 1
    And yes, I agree with the advice of "standalone" execution. I'll go a step further, and think about the nature of the real code that is being tested, and consider mocking too for further isolation and "pure" unit testing. – idjaw Feb 10 '18 at 23:27
  • Thank you, I've taken your advice and will not pass a parameter to the unit test. – Cal Corbin Feb 11 '18 at 00:01