我正在使用一个简单的基于单元测试的测试运行器来测试我的Django应用程序.
我的应用程序本身配置为使用settings.py中的基本记录器:
logging.basicConfig(level=logging.DEBUG)
在我的应用程序代码中使用:
logger = logging.getLogger(__name__)
logger.setLevel(getattr(settings, 'LOG_LEVEL', logging.DEBUG))
但是,在运行单元测试时,我想禁用日志记录,以免它使我的测试结果输出混乱.是否有一种简单的方法可以以全局方式关闭日志记录,以便特定于应用程序的记录器在运行测试时不会将内容写入控制台?
我的Python版本是2.6.
我想只执行一次测试setUp方法,因为我在那里做所有测试都需要的东西.
我的想法是创建一个布尔变量,在第一次执行后将设置为'true',然后禁用多次调用setup方法.
class mySelTest(unittest.TestCase):
    setup_done = False
    def setUp(self):
        print str(self.setup_done)
            
        if self.setup_done:
            return
        self.setup_done = True
        print str(self.setup_done)
输出:
False
True
--- Test 1 ---
False
True
--- Test 2 ---
为什么这不起作用?我错过了什么吗?
如何在从TestCaseunitttest 继承的同一对象中保留更改?
from unittest import TestCase, main as unittest_main
class TestSimpleFoo(TestCase):
    foo = 'bar'
    def setUp(self):
        pass
    def test_a(self):
        self.assertEqual(self.foo, 'bar')
        self.foo = 'can'
    def test_f(self):
        self.assertEqual(self.foo, 'can')
if __name__ == '__main__':
    unittest_main()
即:我希望上面的两个测试通过
Python unittest使用nosetests来试验Python Class和Module fixture,在我的测试中进行最小化设置.
我面临的问题是,我不确定如何使用我的测试中setupUpModule的setUpClass函数和函数中定义的任何变量(例如: - test_1).
这是我用来尝试的:
import unittest
def setUpModule():
    a = "Setup Module variable"
    print "Setup Module"
def tearDownModule():
    print "Closing Module"
class TrialTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print a #<======
        b = "Setup Class variable"
    @classmethod
    def tearDownClass(cls):
        print "Closing Setup Class"
    def test_1(self):
        print "in test 1"
        print a #<======
        print b #<======
    def test_2(self):
        print "in test 2"
    def test_3(self):
        print "in test 3"
    def test_4(self): …我的测试文件基本上是:
class Test(unittest.TestCase):
    def testOk():
        pass
if __name__ == "__main__":
    expensiveSetup()
    try:
        unittest.main()
    finally:
        cleanUp()
但是,我确实希望通过Netbeans测试工具运行我的测试,为此,我需要不依赖于在main中完成的环境设置的单元测试.综观单元测试使用Python安装程序的缓存结果() -它建议使用鼻子.但是,我不认为Netbeans支持这一点.我没有找到任何表明它的信息.另外,我是这里唯一一个实际编写测试的人,所以除非需要,否则我不想为其他2个开发人员引入额外的依赖项.
对于TestSuite中的所有测试,如何进行一次设置和清理?
这里昂贵的设置是创建一些带有虚拟数据的文件,以及设置和拆除一个简单的xml-rpc服务器.我还有2个测试类,一个在本地测试,另一个在xml-rpc上测试所有方法.
我需要为我的测试设置执行顺序,因为我需要在其他测试之前验证一些数据.可以订单吗?
class OneTestCase(unittest.TestCase):
    def setUp(self):
        # something to do
    def test_login (self):
        # first test
        pass
    def test_other (self):
        # any order after test_login
    def test_othermore (self):
        # any order after test_login
if __name__ == '__main__':
    unittest.main()
谢谢
我知道Python中的“ setUp”和“ setUpClass”测试治具,但是我无法找到一种方法来对整个测试套件仅实施一次设置……无论是一个测试还是一个测试类,还是所有测试类都运行。
有人知道这样做的方法吗?
罗伯特
我知道正式的描述:
设置:创建测试的预期状态。
拆卸:进行必要的清理操作。
但是,为什么这是必要的,尤其是在 Benchmark.js 中?为什么需要不同的测试周期(如 Benchmark.js 中所定义)中 Benchmark.js 中所定义)?我观察到,在我能想到的所有情况下(我也认为在所有其他情况下),您可以将设置代码移至准备代码(基准/测试之外的代码),也许将代码拆解到代码末尾,功能本质上是相同的(我也查看了一些 jsperf.com 测试,据我所知,它们也是如此)。
例如,这是我创建的基准测试,该版本使用设置和拆卸:
const bench = new Benchmark(
  'TicTacToeBenchmark',
  // The function to test
  () => {
    ticTacToe.addEvent(
      'turn',
      player => {
        turnText.innerHTML =
          'It\'s ' + (player['id'] === 1 ? 'X' : 'O') + '\'s turn.';
      }
    );
  },
  {
    'setup': () => {
      const players = [
        {
          char: '✕',
          className: 'playerX',
          id: 1,
        },
        {
          char: '◯',
          className: 'playerY',
          id: 2,
        },
      ]; …python ×7
unit-testing ×7
benchmark.js ×1
benchmarking ×1
django ×1
javascript ×1
logging ×1
nose ×1
oop ×1
persistence ×1
testcase ×1
testing ×1