简体   繁体   English

是否可以为所有 LiveServerTestCases 登录一次?

[英]Is it possible to login once for all LiveServerTestCases?

I would like to do something like this:我想做这样的事情:

class TestSuite(LiveServerTestCase):

    @classmethod
    def setUp(self):
        self.driver = webdriver.Chrome()
        
        # Login User
        self.driver.get(self.live_server_url + '/somesite/login/')
        self.driver.find_element(By.ID, "username").click()
        self.driver.find_element(By.ID, "username").send_keys("foo")
        self.driver.find_element(By.ID, "password").click()
        self.driver.find_element(By.ID, "password").send_keys("bar")
        self.driver.find_element(By.ID, "login_button").click()
        # Redirects to home page
    
    @classmethod
    def tearDown(self):
        self.driver.quit()

    def test_foo(self):
        # Do some clicking with already logged in user from home page

    def test_bar(self):
        # Do some clicking with already logged in user from home page

I just feel it is inefficient to call setUp() on every test case.我只是觉得在每个测试用例上调用 setUp() 效率低下。

Any help would be appreciated, thanks...任何帮助将不胜感激,谢谢...

所以将setUpTestSuite移动到LiveServerTestCase

I hate to answer my own question... but I got it working thanks to olli_kahn's hint - extending LiveServerTestCase .我不想回答我自己的问题……但由于 olli_kahn 的提示 - 扩展了LiveServerTestCase我让它工作了。

Steps:脚步:

  • Use setUpClass / tearDownClass (not setUp / tearDown )使用setUpClass / tearDownClass (不是setUp / tearDown
  • Declare setUpClass / tearDownClass as @classmethods (put @classmethod first if using more decorators)setUpClass / tearDownClass声明为@classmethods (如果使用更多装饰器, @classmethod放在首位)
  • Use super() to extend LiveServerTestCase使用super()扩展LiveServerTestCase
  • Use cls instead of self for @classmethod 's to conform to PEP8@classmethod使用cls而不是self以符合 PEP8

Complete code:完整代码:

class TestSuite(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        super(TestSuite, cls).setUpClass()

        cls.driver = webdriver.Chrome()
        
        # Login User
        cls.driver.get(cls.live_server_url + '/somesite/login/')
        cls.driver.find_element(By.ID, "username").click()
        cls.driver.find_element(By.ID, "username").send_keys("foo")
        cls.driver.find_element(By.ID, "password").click()
        cls.driver.find_element(By.ID, "password").send_keys("bar")
        cls.driver.find_element(By.ID, "login_button").click()
        # Redirects to home page
    
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super(TestSuite, cls).tearDownClass()

    def test_foo(self):
        # Do some clicking with already logged in user from home page

    def test_bar(self):
        # Do some clicking with already logged in user from home page

暂无
暂无

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

相关问题 是否可以从.txt打开多个网址并立即抓取所有网页? - Is it possible to open multiple URLs from a .txt and scrape all the webpages at once? 使用 matplotlib,是否可以一次为图形上的所有子图设置属性? - Using matplotlib, is it possible to set properties for all subplots on a figure at once? 为同一文件夹中的所有文件定义一次python导入,可能吗? - define python imports only once for all files in the same folder, is it possible? 是否可以一次从 div class 中的 div 类中抓取所有文本? - Is it possible to scrape all the texts from the div classes within a div class at once? Django:登录时调用一次函数 - Django: Call function once on login 借助ffmpeg的图像转电影功能,是否可以在一段时间内以一次通过帧而不是一次通过? - With ffmpeg's image to movie feature, is it possible to pass in frames over a period of time versus all at once? 是否可以从 SQLite 打印一次表名以及 Python 与该表关联的所有列 - Is it possible from SQLite to print table name once and all columns associated with that table by Python 在Python中,是否可以在没有for循环的情况下一次返回多个3D数组切片? - In Python, is it possible to return multiple 3D array slices all at once without a for loop? 一次返回所有变量 - return all the variables at once 使用 randint() 可以一次获得 4 个数字吗? - Possible to get 4 numbers at once with randint()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM