def CreateGapLease(self): data_file = open('/leases/gap', 'w+') data_file.write(contract_data) data_file.close()def testCreateGapLease(self): contract_writer.CreateGapLease() self.assertEqual(ReadFileContents('/leases/gap'), contract_data)
def testCreateGapLease(self): if os.path.exists(lease_file): RemoveFile(lease_file) ...
def CreateGapLease(self, lease_path=None): if lease_path is None: lease_path = '/leases/gap' ...
def testCreateGapLease(self): lease_file = os.path.join(FLAGS.test_tmpdir, 'gap') contract_writer.CreateGapLease(lease_path=lease_file) self.assertEqual(ReadFileContents(lease_file), contract_data)
What was wrong with "def CreateGapLease(self, lease_path='/leases/gap'):"? Immutable types like strings aren't a risk in default args. My version probably runs slightly faster and will produce much better automatically-extracted docs.The related trap, which you might have been trying to avoid, looks like this:def func(x=):. x.append(5). return x(Except without the silly dots to workaround blogger's formatting)That program only has one list in it which will keep growing every time you call func(). That's where the "x=None .. if x is None: x = " idiom is important.
Nice post.Do yuo have some example of good file system mocking? :)
I assume FLAGS.test_tmpdir is a randomly-named temporary directory (e.g. created with tempfile.mkdtemp), otherwise you still might have problems if you run the test suite concurrently and both instances try to write to the same file at the same time.
The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.