django测试-子类化测试和项目目录结构

tag5nh1u  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(174)

我正在开发一个django应用程序,这是我第一次想用tdd来实现。
这是我的目录结构:

/myproject
    manage.py
    /app1
        models.py
        views.py
        urls.py
        /tests
            test_models.py
            test_forms.py
            test_views.py
    /myproject
        settings.py
        # ... etc.

    /tests
        __init__.py   
        functional_tests.py
        base.py
        visitor.py
        loggedin.py
        member.py

功能测试.py

import unittest

from visitor import VisitorTest
from loggedin import LoggedInTest
from member import SpecialMemberTest 

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

基本.py

import os
from pathlib import Path
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import unittest

# https://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class

class BaseTestCases:

    class BaseTest(unittest.TestCase):

        def get_browser(self, is_headless=False):
            chrome_options = webdriver.ChromeOptions()
            chrome_options.headless = is_headless
            chrome_options.add_argument("--disable-notifications")
            chrome_options.add_argument(f"user-data-dir={str(Path.home())}") # https://stackoverflow.com/questions/31062789/how-to-load-default-pro
            chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")

            browser = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
            return browser        

        def setUp(self): 
            self.browser = self.get_browser()

        def tearDown(self):  
            self.browser.quit()

        def test_common(self):
            pass

登录日期

from base import BaseTestCases

class LoggedInTest(BaseTestCases.BaseTest):
    pass

成员.py

from base import BaseTestCases

class SpecialMemberTest(BaseTestCases.BaseTest):
    pass

访客.py

from base import BaseTestCases

class VisitorTest(BaseTestCases.BaseTest):  

    def test_can_start_a_list_and_retrieve_it_later(self):  
        # Edith has heard about a cool new online to-do app. She goes
        # to check out its homepage
        self.browser.get('http://localhost:8000')

        # She notices the page title and header mention to-do lists
        self.assertIn('To-Do', self.browser.title)  
        self.fail('Finish the test!')  

        # She is invited to enter a to-do item straight away
        #[...rest of comments as before]

可以看出,我正在测试不同类型的用户——他们应该有不同类型的用户体验旅程——这取决于他们的用户类型——然而,所有用户都有一些共同的体验,我已经将这些“共同点”的测试因素考虑在内,以供参考 BaseTestCases.BaseTest .
我有以下问题:
这是django项目中构造功能测试和单元测试的推荐(即最佳实践)方法吗?
子类是否会像我设想的那样工作(即,当子类被“测试”时,父类中的测试也会运行)?
我只实现了一个(失败的)测试(在 VisitorTest )-然而,当我运行测试时,我运行了4个测试,1个失败。为什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题