Python unittest未运行指定的测试

我目前正在通过Python速成课程学习,在测试一章中遇到了问题。我用一把梳子梳了一下,向我想象中的橡皮鸭解释了一下,根本看不到我哪里出了问题。

运行测试文件可以使我“在0.000秒钟内运行0个测试”,但看不到任何错误。 第一个块来自我的文件“ survey.py”,第二个块是测试文件“ testSurvey.py”

我们非常感谢您的帮助。

class AnonymousSurvey():

    def __init__(self,question):
        self.question = question
        self.responses = []

    def showQuestion(self):
        print(self.question)

    def storeResponse(self,newResponse):
        self.responses.append(newResponse)

    def showResults(self):
        print("The survey results are")
        for response in self.responses:
            print("-- " + response)



import unittest

from survey import AnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):
    def TestStoreSingleResponse(self):
        question = "What is your favourite language?"
        mySurvey = AnonymousSurvey(question)
        responses = ["English","Latin","Franglais"]
        for response in responses:
            mySurvey.storeResponse(response)

        for response in responses:
            self.assertIn(response,mySurvey.responses)

unittest.main()
a962319828 回答:Python unittest未运行指定的测试

您的测试方法应以关键字“ test”开头 就像'test_storing_single_response()'

Pytest将以“ test”开头的方法标识为测试用例

签出pytest good practice

    Conventions for Python test discovery
    pytest implements the following standard test discovery:



 - If no arguments are specified then collection starts from testpaths
   (if configured) or the current directory. Alternatively,command line
   arguments can be used in any combination of directories,file names
   or node ids.
 - Recurse into directories,unless they match norecursedirs.
 - In those directories,search for test_*.py or *_test.py files,imported by their test package name.
 - From those files,collect test items:

     -    test prefixed test functions or methods outside of class
     - test prefixed test functions or methods inside Test prefixed test
   classes (without an __init__ method)
本文链接:https://www.f2er.com/3164379.html

大家都在问