简体   繁体   English

具有多个测试类的python单元测试

[英]python unittests with multiple test classes

I am working on Python unittests.我正在研究 Python 单元测试。 I want to segregate my test cases based on the pages/modules.我想根据页面/模块隔离我的测试用例。 When working with unittest.TestCase, every class need to have a setUp method to initialize my page instance.使用 unittest.TestCase 时,每个类都需要有一个 setUp 方法来初始化我的页面实例。 This causes a new instance of browser to open with each test case class run.这会导致浏览器的新实例随着每个测试用例类的运行而打开。 What can I do to run all the testcase classes under the same browserinstance once after the other?我该怎么做才能在同一个浏览器实例下一次又一次地运行所有测试用例类?

logintests.py登录测试.py

browser_input = input("Select a Browser: Firefox, Chrome, IE\n").lower()
class LoginTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):

        browser = BrowserMachine(cls)
        cls.driver = browser.open_browser(cls, browser_input)
        cls.LoginPage = LoginPage(cls.driver)
        cls.HomePages = HomePages(cls.driver)

    def testcaseA(self):

homepagetests.py主页测试.py

class HomepageTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):

        browser = BrowserMachine(cls)
        cls.driver = browser.open_browser(cls, LT.browser_input)
        cls.LoginPage = LoginPage(cls.driver)
        cls.HomePages = HomePages(cls.driver)

    def testcaseB(self):

Make BrowserMachine into a global variable (module-level; you might create a separate module for that. Both classes should then use it.使BrowserMachine成为一个全局变量(模块级;您可以为此创建一个单独的模块。然后两个类都应该使用它。

The BrowserMachine then will not be able to refer to the class ( cls ) but you want only one BrowserMachine anyone, not one per class. BrowserMachine将无法引用该类 ( cls ),但您只需要一个BrowserMachine任何人,而不是每个类一个。

How about creating a main BrowserPage or BrowserMachine class that is a unittest.TestCase and have every other page class inherit that.如何创建一个主要的BrowserPageBrowserMachine类,它是一个 unittest.TestCase 并让所有其他页面类继承它。

That way you can declare all your methods or properties (like defining individualized find_by , click_by , scroll_to etc... actions) in the BrowserPage class.这样你就可以在 BrowserPage 类中声明你的所有方法或属性(比如定义个性化的find_byclick_byscroll_to等...动作)。 Your page classes will be able to call those methods and properties or override them (something like click_login for your LoginTests class, that uses your BrowserPage 's click_by method on a specific login string), have indivudual SetUp and TearDown.您的网页类将能够调用这些方法和属性或重写他们(像click_loginLoginTests类,使用您BrowserPageclick_by方法在特定的登录字符串),有indivudual安装和拆卸。

If you declare your driver in the main classe's setUpClass and quit it in it's tearDownClass, you should be able to use the same webppage across your tests.如果你在主类的 setUpClass 中声明你的驱动程序并在它的 tearDownClass 中quit它,你应该能够在你的测试中使用相同的网页。 Unless you need to spawn new instances of it in other page classes (to perform some action in the background for example, while your main test is running).除非您需要在其他页面类中生成它的新实例(例如,在主测试运行时在后台执行某些操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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