从另一个脚本调用python unittest并导出所有错误消息

2024-06-07 06:17:49 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,基本问题。我使用unittest方法在一个脚本中检查我的模型。现在,我的问题是如何从另一个文件调用这个脚本并保存测试结果。下面是我的代码示例:

**model_test.py**

import unittest
import model_eq #script has models

class modelOutputTest(unittest.TestCase):
    def setUp(self):
        #####Pre-defined inputs########
        self.dsed_in=[1,2]

        #####Pre-defined outputs########
        self.msed_out=[6,24]

        #####TestCase run variables########
        self.tot_iter=len(self.a_in)

    def testMsed(self):
        for i in range(self.tot_iter):
            fun = model_eq.msed(self.dsed_in[i],self.a_in[i],self.pb_in[i])
            value = self.msed_out[i]
            testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("msed",i,value,fun)
self.assertEqual(round(fun,3),round(self.msed_out[i],3),testFailureMessage)

if __name__ == '__main__':
    unittest.main()

下一步是创建另一个名为test_page.py的脚本,该脚本运行单元测试脚本并将结果保存到变量(我需要将结果发布到网页)。

test_page.py    

from model_test.py import *
a=modelOutputTest.testMsed()

但是,我犯了以下错误。

Traceback (most recent call last):
  File "D:\Dropbox\AppPest\rice\Rice_unittest.py", line 16, in <module>
    a= RiceOutputTest.testMsed()
TypeError: unbound method testMsed() must be called with RiceOutputTest instance as first argument (got nothing instead)

有人能给我一些建议吗?谢谢!

谢谢尼克斯的帮助!我的下一个问题是:我需要在一个循环中用两个给定的情况来测试函数。它是贴在here上的。


Tags: inpytestimportself脚本modelunittest
2条回答

您需要使用test runner

test runner A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

from unittest.case import TestCase
import unittest
from StringIO import StringIO
class MyTestCase(TestCase):
    def testTrue(self):
        '''
        Always true
        '''
        assert True

    def testFail(self):
        '''
        Always fails
        '''
        assert False

from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream)
result = runner.run(unittest.makeSuite(MyTestCase))
print 'Tests run ', result.testsRun
print 'Errors ', result.errors
pprint(result.failures)
stream.seek(0)
print 'Test output\n', stream.read()

>>> Output:  
>>> Tests run  2
>>> Errors  []
>>> [(<__main__.MyTestCase testMethod=testFail>,
>>> 'Traceback (most recent call last):\n  File "leanwx/test.py", line 15, in testFail\n                assert False\nAssertionError\n')]
>>> Test output
>>> F.
>>> ======================================================================
>>> FAIL: testFail (__main__.MyTestCase)
>>> ----------------------------------------------------------------------
>>> Traceback (most recent call last):
>>>   File "leanwx/test.py", line 15, in testFail
>>>     assert False
>>> AssertionError
>>>
>>>----------------------------------------------------------------------
>>>Ran 2 tests in 0.001s
>>>
>>>FAILED (failures=1)

从头中删除.py

相关问题 更多 >