简体   繁体   中英

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

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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