1

My test is simple. I want to send two requests to two different servers then compare whether the results match.

I want to test the following things.

  1. send each request and see whether the return code is valid.
  2. compare different portion of the outputs in each test method

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).

Alex
  • 2,915
  • 5
  • 28
  • 38
  • I'd personally send the requests in a test case. You can still split up the checks on the result into several methods; just choose names that don't start with `test`, so they won't be called as test cases. – Sven Marnach Oct 17 '16 at 15:18
  • Possible duplicate of [Difference between setUpClass and setUp in Python unittest](http://stackoverflow.com/questions/23667610/difference-between-setupclass-and-setup-in-python-unittest) – Ari Gold Oct 17 '16 at 15:20
  • Possible duplicate of [\_\_init\_\_ for unittest.TestCase](https://stackoverflow.com/questions/17353213/init-for-unittest-testcase) – Abhijeet Dec 14 '17 at 00:45

1 Answers1

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):
    ...

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

pylover
  • 7,670
  • 8
  • 51
  • 73