【问题标题】:Using Pytest to test an 'if statement' in a DJango module使用 Pytest 测试 DJango 模块中的“if 语句”
【发布时间】:2019-12-16 19:59:46
【问题描述】:

这就是环境

settings.py

from decouple import config

STEMMER = config('STEMMER', default = 'porter')

views.py

from my_project import settings
from nltk import PorterStemmer, LancasterStemmer

if settings.STEMMER == 'porter':
    stemmer = PorterStemmer()
elif settings.STEMMER == 'lancaster':
    stemmer = LancasterStemmer()


def one_function():
    """Do some stuffs with the stemmer"""


def another_function():
    """Do another stuffs with the stemmer"""

问题是如何使用Pytest 创建一个涵盖这两种情况的测试?

提前致谢

【问题讨论】:

    标签: django pytest pytest-django


    【解决方案1】:

    我会将测试分开。然后您可以在测试中编辑设置的值,例如:

    def test():
        settings.STEMMER = "porter"
    

    【讨论】:

    • 感谢您的回答。我已经尝试过了,但它不起作用。此外,尝试(i)创建pytest.fixtures; (ii) 使用了settings 助手记录在here。我相信它不起作用,因为 if 是在模块级别,而不是在功能级别。但是,我不会将 if 语句放在每个方法中。
    猜你喜欢
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 2018-10-31
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2022-10-30
    相关资源
    最近更新 更多