0

I have a test suite function in runTests.py and I run the tests from this file:

def suite():
    suite.addTest(makeSuite(Class1))
    suite.addTest(makeSuite(Class2))
    ...
    suite.addTest(makeSuite(ClassN))

if __name__ == '__main__':
    # grab argument from the command line and
    # initiate myVar1 and myVar2 here before running the suite
    # run the suite here

What I want is to read in arguments from the command line and initiate them inside runTests.py, right before I run the suite in the if statement. These variables would also be used inside of Class1, Class2, etc.

Would it be correct if inside runTests.py I have the following before the suite definition

myVar1 = 'defaultValue'
myVar2 = 'defaultValue'

Then in the if statement grab the arguments from the command line and initialize myVar1 and myVar2. So in the other classes, eg Class1, I would import it like:

from runTests import myVar1
from runTests import myVar2

Basically, what I want is to read in a few parameters from the command line and be able to use those values in the other classes. Thanks!

JiL
  • 65
  • 1
  • 6
  • 1
    "Pass them in" -- or this this *specifically* about a unit-testing scenario? (Also, don't confuse a class with a package.) –  Jun 20 '12 at 16:39
  • this is the scenario I have, I assume it wouldn't be any different even if it was unit-testing, I'm not sure. My tests are actually Selenium Tests. My Class1, Class2 are each a file with one class in it that holds test functions. – JiL Jun 20 '12 at 16:45
  • Because one can often turn a blind eye in the case of unit-testing :) e.g. is the intent a "super global" variable or a "configuration" or ...? The cleanest, IMOHO, is often to pass data *in* to objects (e.g. by the constructor or method call), instead of requiring objects to go out and find data. At the very moment a dependency on `runTests` is added then the classes are no longer valid outside of the testing framework. Also, note that if the `from` occurs outside of something that is delay-executed (e.g. a constructor or method) then it will lead to "cyclic import issue". –  Jun 20 '12 at 17:23
  • However, your conclusion is correct -- that global variables from `runTests` can be accessed after it is `import`ed (using the `from...import` does *not* create aliases, but rather creates a new local binding which will initially name the same object). Do pay attention to the "cyclic import issue" and *order* of operations. –  Jun 20 '12 at 17:34

3 Answers3

0
import sys
params = sys.argv

sys.argv will give you a list

$ python myproram.py var1 var2 var3

sys.argv[0] => myprogram.py

sys.argv[1] => var1

sys.argv[2] => var2

etc

You can access this in any classes

Froyo
  • 17,947
  • 8
  • 45
  • 73
0
from os import sys


class addc:
    def __init__(self , no1 , no2):
        self.no1 = no1
        self.no2 = no2
    def addition(self):
        total = self.no1 + self.no2
        return total



if len (sys.argv) !=3:
    print "no u cant go forwrd"
    print "agument is less"
    sys.exit()

obj_initial = addc(2 , 3 ) 
ans1 = obj_initial.addition() 
print " the total of internla argumnet is %i"%ans1

cmnd_argu1 = int(sys.argv[1]) 
cmnd_argv2 = int (sys.argv[2])

obj_cmnd_argu = addc(cmnd_argu1 , cmnd_argv2)
ans2_cmnd_argu = obj_cmnd_argu.addition()
print " the total of internla argumnet is %i" % int(ans2_cmnd_argu)
warvariuc
  • 57,116
  • 41
  • 173
  • 227
jai
  • 11
  • 1
  • 2
0

I ended up using this from stackoverflow which works. Basically, putting my shared variables myVar1 and myVar2 into a new file myfile.py then import myfile in runTests.py and assign a value to the variables like this:

myfile.myVar1 = sys.argv[1]

In the other files that need to use myVar1, import myfile and use the variable like this:

myfile.myVar1
Community
  • 1
  • 1
JiL
  • 65
  • 1
  • 6