Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1346431828; bh=xwP42Tc+TXtXQtNQXRqdBGnbvzgeU9H/eBa8scTFlqg=; h=Message-ID:Date:From:MIME-Version:To:CC:Subject:References: In-Reply-To:Content-Type:Content-Transfer-Encoding; b=B92G2wNfCkI9nT1Leht6STACH8vb7p5Me742R7wYqZ8hziPPFq/9VM3G8eENQvim2 3/OwmnP0JTHiVGF121Y2PU8Y8j5+9nOiFuUPbgQOuH9PxuSUYCqCmD8NU8E6su19ZW 46gZO65HEefYjzAzZEqCMpRrbcaUzz4yzCXmsr+M= X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '[],': 0.07; 'f.close()': 0.07; 'filename': 0.07; 'python': 0.09; '"w")': 0.09; 'from:name:chris withers': 0.09; 'os.getcwd()': 0.09; 'received:89.151': 0.09; 'received:89.151.125': 0.09; 'received:89.151.125.140': 0.09; 'received:server1.simplistix.co.uk': 0.09; 'received:simplistix.co.uk': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "wouldn't": 0.11; 'empty"': 0.16; 'message- id:@python.org': 0.16; 'paths.': 0.16; 'received:93.89': 0.16; 'received:93.89.128': 0.16; 'received:93.89.128.162': 0.16; 'received:buzzkill.local': 0.16; 'setup(self):': 0.16; 'simplistix': 0.16; 'url:packages': 0.16; 'url:simplistix': 0.16; 'from:addr:python.org': 0.17; 'wrote:': 0.17; 'from:addr:chris': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'absolute': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.28; 'class': 0.29; 'url:python': 0.32; 'skip:s 30': 0.33; 'url:org': 0.36; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'skip:" 10': 0.40; 'url:co': 0.66; 'received:93': 0.72; 'received:89': 0.86 Date: Fri, 31 Aug 2012 17:19:10 +0100 From: Chris Withers User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: Tigerstyle Subject: Re: Unittest - testing for filenames and filesize References: <6b0299df-bc24-406b-8d69-489e990d8e4f@googlegroups.com> In-Reply-To: <6b0299df-bc24-406b-8d69-489e990d8e4f@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346431828 news.xs4all.nl 6903 [2001:888:2000:d::a6]:45888 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28186 On 23/08/2012 12:25, Tigerstyle wrote: > class FileTest(unittest.TestCase): > > def setUp(self): > self.origdir = os.getcwd() > self.dirname = tempfile.mkdtemp("testdir") > os.chdir(self.dirname) I wouldn't change directories like this, it's pretty fragile, just use absolute paths. > def test_1(self): > "Verify creation of files is possible" > for filename in ("this.txt", "that.txt", "the_other.txt"): > f = open(filename, "w") > f.write("Some text\n") > f.close() > self.assertTrue(f.closed) > > def test_2(self): > "Verify that current directory is empty" > self.assertEqual(glob.glob("*"), [], "Directory not empty") > > def tearDown(self): > os.chdir(self.origdir) > shutil.rmtree(self.dirname) Seeing this, you might find the following tools useful: http://packages.python.org/testfixtures/files.html cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk