繁体   English   中英

Python unittest 未运行指定测试

[英]Python unittest not running specified test

我目前正在学习 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()

您的测试方法应以关键字“test”开头,例如“test_storing_single_response()”

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

查看pytest 良好做法

    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)

暂无
暂无

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

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