Skip to content

Is there any way to interfere with global variables from Class BaseCase? #3360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zxczxcdev opened this issue Dec 22, 2024 · 5 comments
Closed
Labels
external Outside SeleniumBase's scope. / Ask somewhere else. question Someone is looking for answers

Comments

@zxczxcdev
Copy link

Like title , My code :

good_count = 0
bad_count = 0


class MyTestClass(BaseCase):        
    def test_swag_labs(self):
        global good_count, bad_count
        try:
            #something here
            
            while True:
                if True:
                    good_count += 1
                    break
                elif False:
                    bad_count += 1
                    break
        finally:
            pass
        

if __name__ == "__main__":
    while True:
        BaseCase.main(__name__, __file__)
        print(good_count, bad_count)

print(good_count, bad_count) >>> good = 0 bad = 0
Please help me. Thank you!

@mdmintz mdmintz added question Someone is looking for answers external Outside SeleniumBase's scope. / Ask somewhere else. labels Dec 22, 2024
@mdmintz
Copy link
Member

mdmintz commented Dec 22, 2024

That's more of a general Python question about global variables:
https://stackoverflow.com/a/423596/7058266
(Add global before the variable when using it inside functions.)

@mdmintz mdmintz closed this as completed Dec 22, 2024
@zxczxcdev
Copy link
Author

class MyTestClass(BaseCase):
def test_swag_labs(self):
global good_count, bad_count

i used

class MyTestClass(BaseCase):        
    def test_swag_labs(self):
        global good_count, bad_count

so is that true ?

@mdmintz
Copy link
Member

mdmintz commented Dec 22, 2024

According to the StackOverflow post, the global variable was first set before the function, and then set with the global keyword in the function:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

@zxczxcdev
Copy link
Author

zxczxcdev commented Dec 22, 2024

al variable was first set bef

According to the StackOverflow post, the global variable was first set before the function, and then set with the global keyword in the function:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

I think I actually did that. Please review my code in topic

**good_count = 0 <<< the global variable was first set before the function,
bad_count = 0 <<< the global variable was first set before the function,**


class MyTestClass(BaseCase):        
    def test_swag_labs(self):
        **global good_count, bad_count**
        try:
            #something here
            
            while True:
                if True:
                    good_count += 1
                    break
                elif False:
                    bad_count += 1
                    break
        finally:
            pass

image

@mdmintz
Copy link
Member

mdmintz commented Dec 22, 2024

Check the Syntax Formats. You're using a pytest structure. pytest handles global variables differently. If you're going to be using a pytest structure, see pytest-dev/pytest#3403 (comment) for handling global variables passed between tests.

You can avoid that by using Syntax Format 23: SeleniumBase SB (Python context manager) so that you're using pure Python format, rather than pytest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external Outside SeleniumBase's scope. / Ask somewhere else. question Someone is looking for answers
Projects
None yet
Development

No branches or pull requests

2 participants