How to initialize a global variable with every class call?

I have module A that looks like this:

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 imported by another module called B. When is the global variable CHECK initialized? When importing? When creating an instance of the class?

I need the CHECK variable to be set every time class A is called. How can I achieve this?

EDIT: I tried the following (which may be what I'm looking for), but CHECK is not set at all inside setUpClass (it remains False, regardless of 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 on how to install CHECK once, each time the test is called?

EDIT: check_function () is definitely called once, but I don’t understand why unittest2.skipIf does not β€œsee” the value set in setUpClass and adheres to the False value specified in the declaration?

DECISION:

The last skeletal part of the code is as follows:

 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) 
+2
source share
3 answers

Your second approach looks like a starting point. But you are faced with the problems of time.

On the one hand, you want this value to be present when using decorators (this is very early), on the other hand, you set it when you call setUpClass() . This is probably quite late.

The only option I like 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 initialized earlier than used.

+1
source

You CHECK variable is initialized when you first enter the module.

See an example. I have a module mymodule.py that 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 

The python invitialization variables are the same command as print and will be executed line by line during the interpreter for the first time in your module.

A clearer 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 
+2
source

How 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 
0
source

Source: https://habr.com/ru/post/1439175/


All Articles