I am a bit new to Python, and for some reason I can't get my head around something.
From the command line I run this
python3 myfile.py
And it works, at the bottom of the file is this, which runs my class, the bit that runs the class is show below (I have just included a bit of the section that calls the rest
if __name__ == "__main__":
dir = os.getcwd()
reportoutputpath="reports"
reportfilename=casedetails['hcname'] + ".html"
......
What I want to do, is run the complete file from my code, I tried this
pathforidefiles="/home/ubuntu/idefiles"
sys.path.append(pathforidefiles)
module = __import__("clean-Fern_Britton_Testcase_01")
This seems to read the file (I have a print line right at the top and that does seem to work, but nothing actually gets executed. I am sure I am missing something fundamental about the way Python works, but I am a bit lost.
Edit I think I could be goint about this the wrong way, and think my question could be. How do I move what is in the main section of the file to me imported into the file that is doing the importing
The file to be imported is like this
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)
Then in the file that is doing the importing
mymodule=importlib.import_module('cleantest')
#code as above
t = unittest.mymodule(exit=False) #to replace t = unittest.main(exit=False)
The error I get is: module 'unittest' has no attribute 'mymodule'
So what do I need to do to make my code (that was in main) to work in my view that is doing the importing?