2015-03-31 18 views
4

我试图在一个Django LiveServerTestCase中运行多个测试。当我运行任何单个测试(与其他人评论),一切都按预期工作。但是当我用两个测试运行测试用例时,第一个工作正常,但第二个用“内部服务器错误”消息加载页面。当我运行多个测试时,Django LiveServerTestCase无法加载页面

代码:

from django.test import LiveServerTestCase 
from selenium.webdriver.firefox.webdriver import WebDriver 


class MyLiveServerTestCase(LiveServerTestCase):  
    """ 
    BaseCleass for my selenium test cases 
    """ 
    @classmethod 
    def setUpClass(cls): 
     cls.driver = WebDriver() 
     cls.url = cls.live_server_url  

     super(MyLiveServerTestCase, cls).setUpClass() 

    @classmethod 
    def tearDownClass(cls): 
     cls.driver.quit() 
     super(MyLiveServerTestCase, cls).tearDownClass() 

class AdminEditFormTest(MyLiveServerTestCase): 
    """ 
    Some test case 
    """ 

    def test_valid_data(self): 
     """ 
     test when user enters correct data 
     """ 
     self.driver.get(self.url) 
     # ... 

    def test_invalid_data(self): 
     """ test when user enters INcorrect data """ 
     self.driver.get(self.url) 
     # ... 

如果我使用close()代替quit()它失败“错误98:地址已在使用”相似,this情况下,除了我只有当我有多个测试在一个LiveServerTestCase错误一个或多个测试案例放在一个.py文件中。

如何在tearDown上创建LiveServerTestCase空闲端口(如果是核心问题)?

有什么解决方法吗?我想要的只是在本地和远程服务器上运行的功能硒测试。

我使用Django 1.6.7,Firefox的37.0,硒2.45.0

UPD

,使用方法,而不是classmethods导致了同样的问题。

def setUp(self): 
    self.driver = WebDriver() 
    self.url = self.live_server_url  

def tearDown(self): 
    self.driver.quit() 
+1

第二次测试是否正常运行? – schillingt 2015-03-31 21:11:31

+0

是的,他们独立运行良好。 – 2015-04-01 06:39:44

+0

我遇到同样的问题,我试图找到一个工作和优雅的解决方案(比单独将每个测试放在它自己的LiveServerTestCase类中更好的东西)。只是为了让更多的数据能够工作,您使用的是哪个版本的Django? – 2015-04-02 08:27:58

回答

1

最后,对于“内部服务器错误”消息的原因是,删除的webdriver从数据库中的所有数据上quit()包括CONTENTTYPES和其他缺省表

这会导致在下次测试开始时加载灯具时出现错误。

N.B.此行为实际上是由于TransactionTestCase(从中继承LiveServerTestCase)在测试运行后重置数据库:it truncates all tables


到目前为止,我的解决办法是与所有的数据在每次试运行(也“默认” Django的数据,例如CONTENTTYPES)加载装置。

class MyLiveServerTestCase(LiveServerTestCase):  
    """ 
    BaseClass for my Selenium test cases 
    """ 
    fixtures = ['my_fixture_with_all_default_stuff_and_testing_data.json'] 

    @classmethod 
    def setUpClass(cls): 
     cls.driver = WebDriver() 
     cls.url = cls.live_server_url  
     super(MyLiveServerTestCase, cls).setUpClass() 

    @classmethod 
    def tearDownClass(cls): 
     cls.driver.quit() 
     super(MyLiveServerTestCase, cls).tearDownClass() 

感谢@help_asap指出这个冲洗数据库上quit()问题!

+0

非常感谢你清理为什么我的第一个'LiveServerTestCase'测试工作正常,但我的第二个给出了这样奇怪的错误。 *注意未来的googlers *,如果你需要一个不执行此操作的'LiveServerTestCase'版本,你可以使用'RealTransactionalLiveServerTestCase(LiveServerTestCase,TestCase):pass'来从'TestCase'获得实际的事务功能。 – CoreDumpError 2017-01-12 00:12:28

+1

@CoreDumpError子类很棒! – po5i 2017-08-23 01:25:11

+0

可能感兴趣的相关Django票证:https://code.djangoproject.com/ticket/23640 – 2017-12-20 10:42:30

相关问题