i used have standalone script unit tests test data in our database. did not use builtin django testing tool, create empty testing database, not want.
in script, created 3 different classes extending unittest.testcase
containing test functions directly executed sql statements.
now prefer able access django orm directly. easiest way via custom management commant (./manage.py datatests
).
in standalone script, call unit tests via following function:
if __name__ == '__main__': unittest.main()
it discover tests in current file , run them.
how can equivalent thing (run test suites) within custom django management command?
i'm sorry not having searched answer long enough before asking, found solution problem myself in stackoverflow answer:
essentially, instead of unittest.main()
following code can used:
suite = unittest.testloader().loadtestsfromtestcase(testcaseclass) unittest.texttestrunner(verbosity=2).run(suite)
this load tests in specified testcaseclass. if want load tests in current module, creating suite way help:
suite = testloader().loadtestsfromname(__name__)
the stackoverflow answer linked above contains full example. furthermore, basic example section of unittest module docs describes same thing. other options load tests, see loading , running tests in docs.
Comments
Post a Comment