English 中文(简体)
如何在 python 单位测试中动态添加测试
原标题:how to add tests dynamically to python unittest

我想设置一个 < code> unitest testCase >, 其中某些案例被动态添加到其中。 方法按我 < code> test_ nothing 所显示的添加, 但是, < code> unitest 没有考虑到它们, 因为它只运行一次测试。 就像我为时过晚而建立我的 < code> test_ xx 。 游戏中的 < code> setUpClass 是否执行得太晚了? 我是否应该在 < code_ init_ / code > 中设置这个方法, 然后调用 < code> super (). init_ {/code >?

import unittest
import blognodes

class Test_base62(unittest.TestCase):
  testset = { 0:  0 , 10:  a , 61:  Z , 62:  10 , 3844:  100 }

  @classmethod
  def setUpClass(cls):
    cls.testme = 5
    print("i am the setUp function")
    for d, b62 in cls.testset.items():
      print("building the first set")
      cls.build_test_base62_values(d, b62)
      print("building the second set")
      cls.build_test_int_values(d, b62)


  @classmethod
  def build_test_base62_values(cls, d, b62):
    def f(cls):
      target = blognodes.base62(d)
      cls.assertEqual(target.str(), b62)
    fname = "test_base62_value_{}".format(d)
    setattr(cls, fname, f)

  @classmethod
  def build_test_int_values(cls, d, b62):
    def f(cls):
      target = blognodes.base62(d)
      cls.assertEqual(target.int(), d)
    fname = "test_int_value_{}".format(d)
    setattr(cls, fname, f)

  def test_nothing(self):
    print("i m test nothing")
    t = dir(self)
    print(t)
    self.assertEqual(5, self.testme)

谢谢

最佳回答

问题是,单位测试的装载器在它即时转换之前的类别上运行一个 dir (), 因此, 在您创建方法的位置上, 方法的创建时间太晚, 方法的创建时间太晚 。

有两种方法可以围绕它工作, 写您自己的运行( ) 方法, 或者使用元类 。 前者意味着您必须重写已经以单位测试方式写入的发现 。 元类实际上并不那么复杂 。 下面我所做的是:

import unittest
import blognodes


class meta_Test_base62(type):
  testset = { 0:  0 , 10:  a , 61:  Z , 62:  10 , 3844:  100 }

  @classmethod
  def __prepare__(mcls, name, bases):
    d = dict()
    d[ testme ] = 5
    for b10, b62 in mcls.testset.items():
      fname = "test_base62_value_{}".format(b10)
      d[fname] = mcls.build_test_base62_values(b10, b62)
      fname = "test_int_value_{}".format(b10)
      d[fname] = mcls.build_test_int_values(b10, b62)
    return d

  @classmethod
  def build_test_base62_values(cls, b10, b62):
    def f(self):
      target = blognodes.base62(b10)
      self.assertEqual(target.str(), b62)
    return f

  @classmethod
  def build_test_int_values(cls, b10, b62):
    def f(self): 
      target = blognodes.base62(b10)
      self.assertEqual(target.int(), b10)
    return f




class Test_base62(unittest.TestCase, metaclass=meta_Test_base62):

  def test_nothing(self):
    self.assertEqual(5, self.testme)
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签