简体   繁体   中英

Divide selenium python files in smaller chunks

I have a few python files for testing my page and now these files have become many and they all share

def setUp(self):
    self.driver = webdriver.Chrome(chrome_options=options)

And this

def tearDown(self):
    self.driver.close()

if __name__ == "__main__":
    unittest.main()

Is it possible to just import these in my other runs? If so, how?

I have tried having this in an own file called start.py and then using

from start import setUp

But it didn't work

Try this, create a base test class and have all your test classes inherit it. Having said, i dont know if the webdriver will play along well (untested)

import unittest

class BaseTest(unittest.TestCase):

    def setUp(self):
        print('--- Setup ---')

    def tearDown(self):
        print('--- Tear Down ---')


class Test1(BaseTest):

    def test_equal(self):
        self.assertEqual(1, 1)


class Test2(BaseTest):

    def test_equal(self):
        self.assertEqual(2, 2)


if __name__ == '__main__':
    unittest.main()

Output:

--- Setup ---
.--- Tear Down ---
--- Setup ---
--- Tear Down ---
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM