mardi 30 juin 2015

proper Python/Django doctest file organization and how to run all django tests at once

I am trying to follow a python tests tutorial at http://ift.tt/1CHAHUp. The wording seems vague, but following it I make a tests dir in my project- by mkdir, not starting an app. It has an init file

mainsite/tests/init.py:

from unit_tst import *

import doctst

__test__={
    'Doctest': doctst
}

mainsite/tests/doctst.py:

"""
This is my worthless test.
>>> print "wee"
wee
>>> print False
False
"""    """
This is my worthless test.
>>> print "wee"
wee
>>> print False
False
"""

mainsite/tests/unit_tst.py:

import unittest

class TestBasic(unittest.TestCase):
    "Basic tests"


    def test_basic(self):
        a = 1
        self.assertEqual(1, a)

    def test_basic2(self):
        a = 1
        assert a == 1

    def test_failure(self):
        b = 2
        assert b == 3

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

The wording is very confusing, as the other says to put these test files under the "tests" directory, then proceeds to keep running ./manage.py test differentappname, when it seems ./manage.py test tests makes sense. Either way, in the main folder ./manage.py test tests yields

Ran 0 tests in 0.000s

OK

but running python tests/unit_tst.py from the main directory works.

[me@localhost site]$ python tests/unit_tst.py
..F
======================================================================
FAIL: test_failure (__main__.TestBasic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/unit_tst.py", line 17, in test_failure
    assert b == 3
AssertionError

----------------------------------------------------------------------
Ran 3 tests in 0.000sscriptcitymirror

My questions are

  1. What is the proper layout for tests? Can all tests for different apps import models from those apps and the test files all live under tests/?

  2. How do you run these tests (why did my command fail)?

Thank you

Aucun commentaire:

Enregistrer un commentaire