2016-10-19 1 views
1

전역 변수에 대한 많은 질문을 보았습니다. 그러나 어떤 이유로 나는 여전히 작동하지 않습니다.Python 전역 변수가 정의되지 않음 - 클래스 내부에 선언되었습니다.

여기 내 시나리오가 있습니다. 테스트하는 응용 프로그램에서 얻을 수있는 다양한 오류 메시지에 대해 다른 기능을 포함하는 개별적인 테스트 사례와 별도의 python 스크립트가 있습니다. 유효성 검사 중 하나가 실패하면 함수가 실패 변수를 증가시키고 기본 테스트 스크립트가 통과 또는 실패인지 확인합니다.

class ErrorValidations: 
    failures = 0 
    def CheckforError1(driver): 
     global failures 
     try: 
      if error1.is_displayed(): 
       failures += 1 

    def CheckforError2(driver): 
     global failures 
     try: 
      if error2.is_displayed(): 
       failures += 1 

    def CheckforError3(driver): 
     global failures 
     try: 
      if error3.is_displayed(): 
       failures += 1 

이것은 검증이 익숙해 곳의 많이 편집 된 예이다 : 테스트가 제대로 변수 실패를 증가되지

from functionslist import ErrorValidations 


def test(driver, browser, test_state): 

    _modules = driver.find_elements_by_xpath('//div[@class="navlink"]') 

    for i in _modules: 
     i.click() 

     ErrorValidations.CheckforError1(driver) 
     ErrorValidations.CheckforError2(driver) 
     ErrorValidations.CheckforError3(driver) 

     if ErrorValidations.failures > 0: 
      driver.report.AppendToReport(i.text, "The " + i.text + "page was not able to load without errors.", "fail", "") 
     else: 
      driver.report.AppendToReport(i.text, "The " + i.text + "page was able to load without errors.", "pass", "") 

내가 오류를 얻을 : 이름이 '실패'가 정의되지 않은, 하지만 다른 정의 해야할지 잘 모르겠습니다.

+3

들여 쓰기를 수정하십시오. 이것은 일회성 클래스 또는 실패 변수일까요? – Dan

+2

첫 번째 코드 조각을 올바르게 읽는다면 실패는 전역 변수가 아니라 클래스 변수입니다. https://docs.python.org/2/tutorial/classes.html – intrepidhero

+0

들여 쓰기가 업데이트되었습니다. – tinneko

답변

1

당신은

대신 글로벌 실패를 사용하는 ErrorValidations 내 전역을 class 속성 '실패'가 아니라 선언하고 시도 :

class ErrorValidations: 
    failures = 0 

    def CheckforError1(driver): 
     try: 
      if error1.is_displayed(): 
       ErrorValidations.failures += 1 

진정한 글로벌이 클래스의 외부에서 선언 할 것이다

관련 문제