2016-12-04 1 views
0

저는 약간 파이썬에 익숙하지 않은데, 어떤 이유로 나는 머리 글자를 쓸 수 없습니다. 클래스를 실행 내 수업을 실행 내가파일 가져 오기 및 실행

그리고이 파일의 맨 아래에서 작동이 인이

python3의 myfile.py에게 실행 명령 줄에서

, 비트는 아래에 보여입니다 (난 그냥 나머지

if __name__ == "__main__": 
    dir = os.getcwd() 
    reportoutputpath="reports" 
    reportfilename=casedetails['hcname'] + ".html" 
    ...... 

내가하고 싶은 것은, 내 코드에서 전체 파일을 실행, 나는이

pathforidefiles="/home/ubuntu/idefiles" 
sys.path.append(pathforidefiles) 
module = __import__("clean-Fern_Britton_Testcase_01") 
을 시도 호출 섹션의 비트를 포함했다

이것은 파일을 읽는 것 같습니다 (맨 위에 인쇄 줄이 있는데 제대로 작동하는 것처럼 보이지만 실제로 실행되지는 않습니다. 나는 파이썬이 작동하는 방식에 대해 근본적인 것이 빠져 있다고 확신하지만, 나는 조금 길다.

편집 나는 이것에 대해 잘못된 생각을 내놓을 수 있으며 내 질문이 될 수 있다고 생각합니다. 어떻게 가져올 수입

파일을하고있는 그 어떤 파일로 가져 나에게 파일의 메인 섹션에 이동하는 일은하고있는 파일에 다음이

class Examplecase01(unittest.TestCase): 
def setUp(self): 
    self.driver = webdriver.Chrome() 
    self.driver.implicitly_wait(30) 
    self.base_url = "http://example.com/" 
    self.verificationErrors = [] 
    self.accept_next_alert = True 

def test_fern_britton_testcase01(self): 
    driver = self.driver 
    .... 

if __name__ == "__main__": 
    dir = os.getcwd() 
    reportoutputpath="reports" 
    reportfilename=casedetails['hcname'] + ".html" 
    outfile = open(dir + "/" + reportoutputpath + "/" + reportfilename, "w") 
    loader = unittest.TestLoader() 
    suite = unittest.TestSuite((
    loader.loadTestsFromTestCase(FernBrittonTestcase01))) 
    runner = HTMLTestRunner(stream=outfile, 
    verbosity=2, 
    title=casedetails['hcname'], 
    description=casedetails['hcdescription']) 
    t = unittest.main(exit=False) 
    print (t.result) 

같다 내가 얻는 수입

mymodule=importlib.import_module('cleantest') 
#code as above 
t = unittest.mymodule(exit=False) #to replace t = unittest.main(exit=False) 

오류 : 모듈에는 속성 '인 MyModule을'이없는 '유닛 테스트'

그래서 내가 무엇에 내 코드를 (즉 주에 있었다)로 설정하려면 어떻게해야합니까 가져 오기를하는 내보기에서 작동합니까?

+0

내가 제대로 이해하면, 당신이 원하는을 해결 'if __name__ == '__main __''부분은 파일을 가져올 때 실행됩니다. 맞습니까? – Leva7

+0

그게 전부입니다. 파일이 명령 행에서 완벽하게 작동하는 것 같습니다. – mozman2

+0

[\\는 무엇을할까요 \ _ \ _ 이름 \ _ \ _ == "\ _ \ _ 메인 \ _ \ _": \'할까요?] (http : /stackoverflow.com/questions/419163/what-does-if-name-main-do) – Leva7

답변

0

내가 실제로하고 싶었던 것에 대해 생각한 후에, 이것이 내가 생각해 낸 것입니다. 나는 또한에 있던 대부분의 코드가 아닌 명령 줄 작업뿐만 아니라, 위의 코드를 수행하는 관점에서

loadfile="my-py-file-that-was-created-and-exported-from-the-IDE" 
sys.path.append("directory-of-where-my-test-case-is") 
mymodule=importlib.import_module(loadfile) 

print(mymodule.casedetails['hcversion']) #I can access values in a dict on the imported file 

#the below then gets the test case from the imported file 
suite = unittest.TestSuite((loader.loadTestsFromTestCase(mymodule.Testcase01))) 

에서, 사이트에서이 작업을 실행 만 정말 관심 원래 테스트 케이스의 메인 섹션

나는 질문 \ 다른 문제가 있지만,이 하나가

감사

그랜트