【问题标题】:Invoked method from Python unit test issues从 Python 单元测试问题调用的方法
【发布时间】:2016-03-29 17:42:39
【问题描述】:

我正在尝试调用以下方法,post_sample(文件名 index.py):

class Main(Resource):
    @app.route('/sample', methods=['POST'])
    def post_sample():
        return 'POST received'

来自以下单元测试代码:

def test_post():
    Main().post_sample()
    print 'test_here'

但是当我运行时出现以下错误,我无法理解:

>       Main().post_sample()
E       TypeError: post_sample() takes no arguments (1 given)

tests/test_post.py:8: TypeError

TIA!

【问题讨论】:

  • 为什么你的视图函数周围有一个类?
  • 您是否使用test client 进行单元测试?
  • @wgwz 因为我有一些函数,所以我将它们封装在一个类中。
  • @idjaw 不,我正在使用 py.test
  • @mr-sk 查看 Flask 的测试客户端,它做得非常好,将有助于您的单元测试。

标签: python unit-testing flask python-unittest


【解决方案1】:

post_sample 是一种实例方法,因此必须至少接受一个参数,即对实例的引用:

def post_sample(self):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    相关资源
    最近更新 更多