Reputation: 731
I have a module A who looks like the following:
import unittest2
def check_function():
# performs actions and returns True or False
return smth
CHECK = check_function()
class A(unittest2.TestCase):
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
Module A is being imported by another module, called B. When is the global variable CHECK initialized? At import? At class instantiation?
I need to have the CHECK variable set each time class A is called. How can I achieve this?
EDIT: I have tried the following (which might be what I am looking for), but CHECK is not set at all inside setUpClass (it stays False, no matter what check_function() returns).
import unittest2
def check_function():
# performs actions and returns True or False
return smth
CHECK = False
class A(unittest2.TestCase):
global CHECK
@classmethod
def setUpClass(cls):
CHECK = check_function()
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
Any ideas of how to set CHECK once, each time the test is invoked?
EDIT: check_function() is certainly called once, but I don't understand why unittest2.skipIf does not "see" the value set in setUpClass, and sticks to the False value set at declaration?
SOLUTION:
The final skeletopn of the code looks like this:
import unittest2
def check_function():
# performs checks and returns True or False
return smth
class A(unittest2.TestCase):
CHECK = check_function()
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# do tests
self.assertTrue(True)
@unittest2.skipIf(CHECK, "skip message 1")
def test_2(self):
# do tests
self.assertTrue(True)
Upvotes: 2
Views: 2636
Reputation: 91027
Your 2nd approach looks like a starting point. But you are running into timing problems.
On one hand, you want that value to be present on applying the decorators (that's VERY early), on the other hand, you setting it when calling setUpClass()
. That is probably quite late.
The only option seems to me to do
class A(unittest2.TestCase):
CHECK = check_function()
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
where CHECK
is initialized earlier than used.
Upvotes: 1
Reputation: 9533
What about this:
import unittest2
def check_function():
# performs actions and returns True or False
return smth
CHECK = None
class A(unittest2.TestCase):
def __init__(self,*args,**kwargs):
CHECK=check_funtion
super(A,self).__init__(*args,**kwargs)
@unittest2.skipIf(CHECK, "skip message 1")
def test_1(self):
# test check
@unittest2.skipIf(CHECK, "skip message 2")
def test_2(self):
# test check
Upvotes: 0
Reputation: 27611
You CHECK
variable initialized when you module import first time.
See example. I have module mymodule.py
which contains:
print "i am mymodule"
And some anothermodule.py
:
print "first"
import mymodule
print "second"
import mymodule
print "third"
When I run another module.py
I get:
first
i am mymodule
second
third
Invitialization variables in python is the same command as print
and
will be performed line-by-line at the time of the interpreter will be the first time in your module.
More clear example.
mymodule.py
:
def get_a():
print 'init a'
return 42
def get_b():
print 'init b'
return 20
a = get_a()
b = get_b()
anothermodule.py
from mymodule import a
print "a =", a
print "b =", b
Result:
init a
init b
a = 42
Traceback (most recent call last):
File "anothermodule.py", line 3, in <module>
print b
NameError: name 'b' is not defined
Upvotes: 2