Skip to content

Commit

Permalink
Use metaclass for test generation, works way better for test discovery.
Browse files Browse the repository at this point in the history
Test - naming, bikeshedding.
  • Loading branch information
stuaxo committed Apr 29, 2016
1 parent 9aa9a08 commit 93ccd6a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 54 deletions.
54 changes: 0 additions & 54 deletions tests/unittests/file_output.py

This file was deleted.

16 changes: 16 additions & 0 deletions tests/unittests/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest

if __name__ == '__main__':

# use the default shared TestLoader instance
test_loader = unittest.defaultTestLoader

# use the basic test runner that outputs to sys.stderr
test_runner = unittest.TextTestRunner()

# automatically discover all tests in the current dir of the form test*.py
# NOTE: only works for python 2.7 and later
test_suite = test_loader.discover('.')

# run the test suite
test_runner.run(test_suite)
File renamed without changes.
50 changes: 50 additions & 0 deletions tests/unittests/test_file_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Check if shoebot can create files in all it's different output formats
and that none are zero bytes long.
"""

import tempfile
import unittest

from os import stat, unlink
from os.path import exists

import shoebot

from shoebot.core import CairoCanvas, CairoImageSink
from shoebot.data import BezierPath
from shoebot.grammar import NodeBot


FORMATS = ["png", "ps", "pdf", "svg"]
BOT_CODE = "background(0)"

def run_shoebot_code(code, outputfile):
bot = shoebot.create_bot(outputfile=outputfile)
bot.run(code)
return outputfile


class TestFileOutputMeta(type):
def __new__(mcs, name, bases, dct):
def create_output_test(fmt, name):
def test(self):
h, fn = tempfile.mkstemp(suffix=".%s" % fmt)
run_shoebot_code(BOT_CODE, outputfile=fn)
self.assertTrue(exists(fn), "%s was not created" % fn)
size_not_zero = stat(fn).st_size != 0
self.assertTrue(size_not_zero, "%s is zero bytes." % fn)
unlink(fn)
return test

for fmt in FORMATS:
test_name = 'test_create_' + fmt
dct[test_name] = create_output_test(fmt, name)

return type.__new__(mcs, name, bases, dct)

class TestSequence(unittest.TestCase):
__metaclass__ = TestFileOutputMeta

if __name__ == '__main__':
unittest.main()

0 comments on commit 93ccd6a

Please sign in to comment.