简体   繁体   English

如何在 pytest 中通过测试?

[英]how can I pass a test in pytest?

I need to pass this test.我需要通过这个测试。 In my test file I can't get results with tense "present" I'm just getting past "past" and "future" tense.在我的测试文件中,我无法获得“现在”时态的结果,我只是过去了“过去”和“未来”时态。 In the case of present, 'assertion error' always appears.在当前的情况下,总是出现“断言错误”。

    if tense == "past":
        verbs = ["drank", "ate", "grew", "laughed", "thought", "ran", "slept", "talked", "walked", "wrote"]
        verb = random.choice(verbs)
        return verb
    if tense == "present" and grammatical_number == 1:
        verbs = ["drinks", "eats", "grows", "laughs", "thinks", "runs", "sleeps", "talks", "walks", "writes"]
        verb = random.choice(verbs)
        return verb
    if tense == "present" and grammatical_number == 2:
        verbs = ["drink", "eat", "grow", "laugh", "think", "run", "sleep", "talk", "walk", "write"]  
        verb = random.choice(verbs)
        return verb 
    if  tense == "future":
        verbs = ["will drink", "will eat", "will grow", "will laugh","will think", "will run", "will sleep", "will talk","will walk", "will write"]
        verb = random.choice(verbs)
        return verb`

And here is the result in my test_sentence这是我的 test_sentence 中的结果

def test_get_verb():
    verbs = ["drank", "ate", "grew", "laughed", "thought", "ran", "slept", "talked", "walked", "wrote"]
    for _ in range(4):
        word = get_verb(1, tense = "past")
        assert word in verbs

    verbs = ["will drink", "will eat", "will grow", "will laugh","will think", "will run", "will sleep", "will talk","will walk", "will write"]
    for _ in range(4):
        word = get_verb(1, tense = "future")
        assert word in verbs

I have a feeling that you just mixed up the grammatical_number .我有一种感觉,你只是混淆了grammatical_number If I use this, the tests pass.如果我使用它,则测试通过。

import random

import pytest


def get_determiner(
        grammatical_number
):
    if grammatical_number == 1:
        words = ["the", "one"]
        word = random.choice(words)
        return word

    if grammatical_number == 2:
        words = ["some", "many"]
        word = random.choice(words)
        return word


def get_noun(
        grammatical_number
):
    if grammatical_number == 1:
        nouns = ["adult", "bird", "boy", "car", "cat", "child", "dog", "girl",
                 "man", "woman"]
        noun = random.choice(nouns)
        return noun
    if grammatical_number == 2:
        nouns = ["adults", "birds", "boys", "cars", "cats", "children", "dogs",
                 "girls", "men", "women"]
        noun = random.choice(nouns)
        return noun


def get_verb(
        grammatical_number,
        tense
):
    if tense == "past":
        verbs = ["drank", "ate", "grew", "laughed", "thought", "ran", "slept",
                 "talked", "walked", "wrote"]
        verb = random.choice(verbs)
        return verb
    if tense == "present" and grammatical_number == 1:
        verbs = ["drinks", "eats", "grows", "laughs", "thinks", "runs",
                 "sleeps", "talks", "walks", "writes"]
        verb = random.choice(verbs)
        return verb
    if tense == "present" and grammatical_number == 2:
        verbs = ["drink", "eat", "grow", "laugh", "think", "run", "sleep",
                 "talk", "walk", "write"]
        verb = random.choice(verbs)
        return verb
    if tense == "future":
        verbs = ["will drink", "will eat", "will grow", "will laugh",
                 "will think", "will run", "will sleep", "will talk",
                 "will walk", "will write"]
        verb = random.choice(verbs)
        return verb


def main():
    print(f"{get_determiner(1)} {get_noun(1)} {get_verb(1, 'past')}")
    print(f"{get_determiner(2)} {get_noun(2)} {get_verb(2, 'past')}")
    print(f"{get_determiner(1)} {get_noun(1)} {get_verb(1, 'present')}")
    print(f"{get_determiner(2)} {get_noun(2)} {get_verb(2, 'present')}")
    print(f"{get_determiner(1)} {get_noun(1)} {get_verb(1, 'future')}")
    print(f"{get_determiner(2)} {get_noun(2)} {get_verb(2, 'future')}")


main()


def test_get_determiner():
    # 1. Test the single determiners.

    single_determiners = ["the", "one"]

    # This loop will repeat the statements inside it 4 times.
    # If a loop's counting variable is not used inside the
    # body of the loop, many programmers will use underscore
    # (_) as the variable name for the counting variable.
    for _ in range(4):
        word = get_determiner(1)

        # Verify that the word returned from get_determiner is
        # one of the words in the single_determiners list.
        assert word in single_determiners

    # 2. Test the plural determiners.

    plural_determiners = ["some", "many"]

    # This loop will repeat the statements inside it 4 times.
    for _ in range(4):
        word = get_determiner(2)
        assert word in plural_determiners


def test_get_noun():
    # 1. Test the single nouns.

    single_nouns = ["adult", "bird", "boy", "car", "cat", "child", "dog",
                    "girl", "man", "woman"]

    # This loop will repeat the statements inside it 4 times.
    # If a loop's counting variable is not used inside the
    # body of the loop, many programmers will use underscore
    # (_) as the variable name for the counting variable.
    for _ in range(4):
        word = get_noun(1)

        # Verify that the word returned from get_noun is
        # one of the words in the single_nouns list.
        assert word in single_nouns

    # 2. Test the plural nouns.

    plural_nouns = ["adults", "birds", "boys", "cars", "cats", "children",
                    "dogs", "girls", "men", "women"]

    # This loop will repeat the statements inside it 4 times.
    for _ in range(4):
        word = get_noun(2)
        assert word in plural_nouns


def test_get_verb():
    verbs = ["drank", "ate", "grew", "laughed", "thought", "ran", "slept",
             "talked", "walked", "wrote"]
    for _ in range(4):
        word = get_verb(1, tense="past")
        assert word in verbs

    verbs = ["will drink", "will eat", "will grow", "will laugh", "will think",
             "will run", "will sleep", "will talk", "will walk", "will write"]
    for _ in range(4):
        word = get_verb(1, tense="future")
        assert word in verbs

    verbs = ["drinks", "eats", "grows", "laughs", "thinks", "runs", "sleeps",
             "talks", "walks", "writes"]
    for _ in range(4):
        word = get_verb(1, tense="present")
        assert word in verbs

    verbs = ["drink", "eat", "grow", "laugh", "think", "run", "sleep",
             "talk", "walk", "write"]
    for _ in range(4):
        word = get_verb(2, tense="present")
        assert word in verbs


pytest.main(["-v", "--tb=line", "-rN", __file__])

Assertion error is just that your assert is not passing and you are getting a lot of explanation why in the error message.断言错误只是您的断言没有通过,并且您在错误消息中得到了很多解释原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM