[英]Setup method for nosetest. (Test class)
I am mocking out a database in some tests that I am doing. 我在一些我正在做的测试中嘲笑数据库。 How would I create a setup method for the entire class, such that it runs each time an individual test within the class runs?
如何为整个类创建一个设置方法,以便每次在类中运行单个测试时它都会运行?
Example of what I am attempting to do. 我试图做的例子。
from mocks import MockDB
class DBTests(unittest.TestCase):
def setup(self):
self.mock_db = MockDB()
def test_one(self):
#deal with self.mock_db
def test_two(self):
#deal with self.mock_db, as if nothing from test_one has happened
I'm assuming a teardown method would also be possible, but I can't find documentation that will do something like this. 我假设一个拆解方法也是可能的,但我找不到会做类似这样的事情的文档。
If you are using Python unit test framework something like this is what you want: 如果您正在使用Python单元测试框架,那么这就是您想要的:
class Test(unittest.TestCase):
def setUp(self):
self.mock_db = MockDB()
def tearDown(self):
pass # clean up
def test_1(self):
pass # test stuff
With Nose, subclassing of TestCase works the same way as standard unittest
-- setUp
/ tearDown
are the same. 使用Nose,TestCase的子类化与标准
unittest
工作方式相同 - setUp
/ tearDown
是相同的。 From the nose docs 从鼻子文档
Test classes
测试课程
A test class is a class defined in a test module that matches testMatch or is a subclass of unittest.TestCase.
测试类是在测试模块中定义的类,它匹配testMatch或是unittest.TestCase的子类。 All test classes are run the same way: Methods in the class that match testMatch are discovered, and a test case is constructed to run each method with a fresh instance of the test class.
所有测试类都以相同的方式运行:发现类中与testMatch匹配的方法,并构造一个测试用例,以使用测试类的新实例运行每个方法。 Like unittest.TestCase subclasses, other test classes can define setUp and tearDown methods that will be run before and after each test method.
与unittest.TestCase子类一样,其他测试类可以定义将在每个测试方法之前和之后运行的setUp和tearDown方法。 Test classes that do not descend from unittest.TestCase may also include generator methods and class-level fixtures.
不从unittest.TestCase下降的测试类也可能包括生成器方法和类级别的fixture。 Class-level setup fixtures may be named setup_class, setupClass, setUpClass, setupAll or setUpAll;
类级设置装置可以命名为setup_class,setupClass,setUpClass,setupAll或setUpAll; teardown fixtures may be named teardown_class, teardownClass, tearDownClass, teardownAll or tearDownAll.
拆卸夹具可以命名为teardown_class,teardownClass,tearDownClass,teardownAll或tearDownAll。 Class-level setup and teardown fixtures must be class methods.
类级设置和拆卸夹具必须是类方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.