Normally this sort of thing would be in a setUp(self): method, which would create a class variable of a file-like object before each test, which could then be used in that test.