0

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?

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
mozman2
  • 911
  • 1
  • 8
  • 17
  • If I understand you correctly, you want the `if __name__ == '__main__'` part executed when you import the file, right? – illright Dec 04 '16 at 14:32
  • That is it. The file seems to work perfectly on the command line – mozman2 Dec 04 '16 at 14:35
  • Possible duplicate of [What does \`if \_\_name\_\_ == "\_\_main\_\_":\` do?](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) – illright Dec 04 '16 at 14:38
  • How do you plan to run the tests? Do you want to be able to run them from both files? – illright Dec 05 '16 at 16:30

1 Answers1

0

After some thought on what I actually wanted to do, this is what I came up with (It works). I am only really interested in running this from the site, not from the command line

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

In the view that does the work, as well as the above code, I also have most of the code that was in the main section of the original test case

I have other issues\questions, but this one is solved

Thanks

Grant

mozman2
  • 911
  • 1
  • 8
  • 17