相关疑难解决方法(0)

是否有一种简单,优雅的方式来定义单身人士?

似乎有很多方法可以在Python中定义单例.Stack Overflow是否有共识?

python singleton design-patterns

427
推荐指数
13
解决办法
27万
查看次数

单元测试setUp/tearDown进行多次测试

是否有一个在测试场景的开头/结尾触发的函数?函数setUp和tearDown在每次测试之前/之后触发.

我通常想拥有这个:

class TestSequenceFunctions(unittest.TestCase):

    def setUpScenario(self):
        start() #launched at the beginning, once

    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)

    def tearDownScenario(self):
        end() #launched at the end, once
Run Code Online (Sandbox Code Playgroud)

现在,这些setUp和tearDown是单元测试并在我的所有场景中传播(包含许多测试),一个是第一个测试,另一个是最后一个测试.

python unit-testing

107
推荐指数
3
解决办法
7万
查看次数

更新到Django 1.8 - AttributeError:django.test.TestCase没有属性'cls_atomics'

我将Django 1.7项目更新为Django 1.8,现在运行测试时出现错误(这是子类django.test.TestCase).

Traceback (most recent call last):
  File "env\lib\site-packages\django\test\testcases.py", line 962, in tearDownClass
cls._rollback_atomics(cls.cls_atomics)
  AttributeError: type object 'SomeTests' has no attribute 'cls_atomics'
Run Code Online (Sandbox Code Playgroud)

如果我通过测试进行调试,我可以毫无问题地遍历所有行,但是在最后一行之后抛出异常.

这是一个示例测试:

import django
import unittest
from django.test import TestCase
import logging
import sys
from builtins import classmethod, isinstance

class ATestTests(TestCase):

    @classmethod
    def setUpClass(cls):
        django.setup()
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)


    def setUp(self):
        self._app = Application(name="a")


    def testtest(self):

        self.assertIsNotNone(self._app)
Run Code Online (Sandbox Code Playgroud)

我的环境:

astroid==1.3.4
colorama==0.3.3
defusedxml==0.4.1
Django==1.8
django-extensions==1.5.2
django-filter==0.9.2
djangorestframework==3.0.5
djangorestframework-xml==1.0.1
eight==0.3.0
future==0.11.4
logilab-common==0.63.2
Markdown==2.5.2
pylint==1.4.1
python-dateutil==2.4.1
python-mimeparse==0.1.4
six==1.9.0
xmltodict==0.9.2
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

django python-3.x django-1.8

60
推荐指数
3
解决办法
7710
查看次数

所有测试后,Python单元测试运行功能

我需要通过ssh在python上测试smth.我不想为每个测试做ssh连接,因为它很长,我写了这个:

class TestCase(unittest.TestCase):
    client = None
    def setUp(self):
        if not hasattr(self.__class__, 'client') or self.__class__.client is None:
            self.__class__.client = paramiko.SSHClient()
            self.__class__.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.__class__.client.connect(hostname=consts.get_host(), port=consts.get_port(), username=consts.get_user(),
                                password=consts.get_password())

    def test_a(self):
        pass

    def test_b(self):
        pass

    def test_c(self):
        pass

    def disconnect(self):
        self.__class__.client.close()
Run Code Online (Sandbox Code Playgroud)

和我的跑步者

if __name__ == '__main__':
    suite = unittest.TestSuite((
        unittest.makeSuite(TestCase),
    ))
    result = unittest.TextTestRunner().run(suite)
    TestCase.disconnect()
    sys.exit(not result.wasSuccessful())
Run Code Online (Sandbox Code Playgroud)

在这个版本中我得到错误TypeError: unbound method disconnect() must be called with TestCase instance as first argument (got nothing instead).那么在所有测试通过后我怎么能断断续续?最诚挚的问候.

python ssh unit-testing python-unittest

9
推荐指数
2
解决办法
5547
查看次数