Django StaticLiveServerTestCase不显示分组测试中的数据

时间:2018-08-21 19:47:20

标签: django testing functional-testing factory-boy

我正在使用Selenium为Django 1.9编写的应用程序编写功能测试。

我有一个函数populate_test_db(),它使用factory_boy创建了一些测试数据:

def populate_test_db(self):

"""
Adds records to an empty test database
"""
self.coder = UserFactory.create()
self.country = CountryFactory.create()
self.assignment = AssignmentFactory.create()
self.assignment_progress = AssignmentProgressFactory.create(assignment = self.assignment)
self.article = ArticleFactory.create(assignments = (self.assignment,))
self.articles = ArticleFactory.create_batch(100, assignments = (self.assignment,))

return self

在为我的应用设置功能测试时,我调用此函数:

from functional_tests.testing_utilities import populate_test_db

class NewUserTest(StaticLiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        populate_test_db(self)

(我也尝试过使用setUpTestData(),但这似乎对StaticLiveServerTestCase也无效)。

然后我运行以下两个测试:

def test_can_login_to_site(self):

    self.browser.get(self.live_server_url)

    username = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type="text"]') 
    username.send_keys('bobby')

    password = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input[type="password"]') 
    password.send_keys('very_secure')
    password.send_keys(Keys.ENTER)
    time.sleep(1)

    assignment = self.browser.find_element_by_css_selector('#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')
    self.assertIn(self.assignment.country.state_name, assignment.text)

def test_can_access_assignment(self):
    print(self.coder)
    print(self.assignment)
    self.browser.get(self.live_server_url)

    username = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type="text"]') 
    username.send_keys('bobby')

    password = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input[type="password"]') 
    password.send_keys('very_secure')
    password.send_keys(Keys.ENTER)
    time.sleep(1)

    assignment = self.browser.find_element_by_css_selector('#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')
    assignment.click()
    time.sleep(1)

    headline = self.browser.find_element_by_css_selector('#article-info > h2')
    self.assertIn(self.article.headline, headline.text)

两个测试都独立通过(即,当另一个测试被注释掉时),但是当两个测试同时处于活动状态时,则不会通过。当两者都处于活动状态时,其中一个失败并显示以下消息:

File "path/to/app/functional_tests/test_functional_tests.py", line 64, in test_can_access_assignment
assignment = self.browser.find_element_by_css_selector('#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')
....
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a"}

即使在上一个测试中出现了 did 相同的元素(因此第一个测试通过了,第二个测试没有通过)。这是为什么?

0 个答案:

没有答案