未绑定的方法equaldigits()必须以sut instance作为第一个参数调用(改为int instance)



我有一个名为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,请帮助。

在我从sut.py文件中删除类名并按照brenn的建议使用import sut之后,代码正常工作了。

最新更新