unbound方法equaldigits()必须使用sut实例作为第一个参数调用(改为使用int实例)

时间:2014-06-03 06:51:50

标签: python testing module

我有一个名为sut.py的模块,我想通过将其导入另一个文件来测试它。

import logging

class sut:
  logging.basicConfig(filename='run.log',level=logging.INFO)

      def equaldigits(a, b):

        try:
          c = a - b
          return c
          logging.info('%s is the value', str(c))
        except ValueError:
          c = 'Unable to successfully complete execution'
          print c
          logging.info(c)  

我有另一个程序来测试它,我想在那里调用模块。

import time
import logging
from sut import *

# Test Method
def test(actual, expected):
  if actual == 'Unable to successfully complete execution':
    prefix = ' ERROR '
  elif actual == expected:
    prefix = ' PASS '
  else:
    prefix = '  FAIL '
  print '%s actual: %s expected: %s' % (prefix, repr(actual), repr(expected))


# Provided main() calls the above functions,
# using test() to check if each result is correct or not.
def main():
  print 'SUT'
  start_time = time.time()
  test(sut.equaldigits(0, 0), 0)
  print time.time() - start_time, "seconds"


# Standard boilerplate to call the main() function.
if __name__ == '__main__':
  main()

但是我遇到了这个问题

usxxtomaram1:basic tomara$ ./sut_test.py 
SUT
Traceback (most recent call last):
  File "./sut_test.py", line 45, in <module>
    main()
  File "./sut_test.py", line 39, in main
    test(sut.equaldigits(0, 0), 0)
TypeError: unbound method equaldigits() must be called with sut instance as first argument (got int instance instead)

我只是想学习python,请帮助。

1 个答案:

答案 0 :(得分:0)

在我从sut.py文件中删除了类名后,代码工作了,并且还使用了import sut,正如Bren建议的那样。