Python:在unittest框架的测试用例中将一次性初始化放在哪里?

Ale*_*lex 1 python unit-testing python-2.7 python-unittest

我的测试很简单。我想向两个不同的服务器发送两个请求,然后比较结果是否匹配。

我要测试以下内容。

  1. 发送每个请求,并查看返回码是否有效。
  2. 比较每种测试方法中输出的不同部分

I do not want to send the requests in setUp method because it will be send over and over for each new test. I would rather want to send the requests at the initialization. (maybe in the init method). But I found a lot of people were against that idea because they believe I should not override the init method for some reason. (I do not know exactly why) If that's the case, where should I send the requests? I am kind of against doing them in the class body (as shared variables).

pyl*_*ver 5

A class method called before tests in an individual class run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def setUpClass(cls):
    ...
Run Code Online (Sandbox Code Playgroud)

See: https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUpClass