使用Pytest在DJango模块中测试'if语句'

时间:2019-12-16 19:59:46

标签: django pytest pytest-django

这是环境

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创建测试?

预先感谢

1 个答案:

答案 0 :(得分:0)

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

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