2016-12-04 8 views
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」

を持っていない「ユニットテスト」だから私はに(それがメインであった)私のコードを作るために何をすべきかが必要ですインポートを行っている私の見解で働く?

+0

私が正しくあなたを理解していれば、あなたが希望を解かれます'if __name__ == '__main __''部分は、ファイルをインポートするときに実行されます。 – Leva7

+0

それです。 (HTTP:/:ファイルは、[\ 'のですか?何\'もし\ _ \ _名\ _ \ _ == "\ _ \ _メイン\ _ \ _"]のコマンドライン – mozman2

+0

可能性のある重複した上で完璧に動作するようです/stackoverflow.com/questions/419163/what-does-if-name-main-do) – Leva7

答えて

0

私は実際にやりたいことのいくつかの考えた後に、これは私が(それが動作する)思い付いたものです。 Iないコマンドラインから、サイトからこれを実行しているのみで、本当に興味を持って仕事だけでなく、上記のコードを実行し鑑み

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))) 

、私もしていたコードのほとんどを持っています元のテストケースの要部

私は質問\他の問題を持っていますが、この1つは

おかげ

グラント

関連する問題