0
我對Python有點新,因爲某些原因,我無法擺脫困境。導入並運行一個文件
從命令行運行此
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")
部分的
這似乎讀取文件(我有一個打印線在頂部,似乎工作,但沒有實際得到執行。我相信我對Python的工作方式缺少一些基本的東西,但我有點迷失。
編輯 我想我可能會錯誤地認爲這是錯誤的方式,並認爲我的問題可能是。如何移動是什麼文件給我的主要部分導入的文件,該文件是做進口
的要導入的文件是這樣的
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的」
那麼,我需要做的,使我的代碼(這是在主)以在我看來是在做導入工作?
如果我理解正確的話,你想在導入文件時執行'if __name__ =='__main __''部分,對吧? – Leva7
就是這樣。該文件似乎在命令行上完美工作 – mozman2
[\ if \ _ \ _ name \ _ \ _ == \ \ _ \ _ main \ _ \ _「可能重複:\'do?](http:/ /stackoverflow.com/questions/419163/what-does-if-name-main-do) – Leva7