0

There is a problem when I test my Model using unittest module,which is that nothing was displayed when the process was succeed except for a string came from the "print".the result of my Iteration of the users didnt show up.What i was dong is below:

1,models.py

    class UserManager(models.Manager):
      def hah(self):
        return None
    class User(models.Model):
                id = models.IntegerField(primary_key=True)  
                email = models.CharField(unique=True, max_length=50)
                password = models.CharField(max_length=50)
                logintime = models.IntegerField(default='')
                registertime = models.IntegerField()
                modifytime = models.IntegerField()
                loginip = models.CharField(max_length=60)
                islock = models.IntegerField()
                isavtive = models.IntegerField()
                activecode = models.CharField(max_length=45)
                role = models.CharField(max_length=1)

                objects = UserManager()
                class Meta:
                    db_table = 'user'

2,serializers.py

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','email','password','loginip')

3,tests.py

class UserModelTest(unittest.TestCase):
    def get_user(self):
        users = User.objects.all()
        userserializer = UserSerializer(users,many=True)
        print(userserializer.data)
        print(users)
        for row in users:
            print(row.email)
        print("this is user model test")

When I ran python3.4 ./manage.py test user.tests.UserModelTest.get_user ,the results were:

Creating test database for alias 'default'...
[]
[]
this is user model test
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Destroying test database for alias 'default'...

Did i miss something or any misunderstanding about unittest? And I notice someone has the same problem,what they are suggested to do is add -- -s option,but not work for me.And any solutions? Thanks in advance!

4
  • 1
    Doesn't unit test look for methods that begin with "test_"? What happens if you change the name to test_get_user? Commented Apr 2, 2015 at 10:37
  • yeah,it does,but it can be specified to a full path even without a prefix "test_",and i tried your method,but not work! Commented Apr 2, 2015 at 10:46
  • Do you have a setUp method instantiating objects in the test database? Commented Apr 2, 2015 at 10:50
  • no,i didnt have one,i just noticed this basic for testing,but,can unittest use the actual database? Commented Apr 2, 2015 at 11:04

1 Answer 1

3

It's the correct display but you didn't write any tests.
There are few things you should know to be able to write Unit tests:

  1. your test functions should start with the word test such as test_get_user_query, this is to make your tests callable from:
    ./manage.py test appName so you don't have to reference the whole path to the test.
  2. The database used in unit tests is isolated from your development database, which means that it's completely empty and you need to populate it each time you want to test it, for your example you should add a line like this at the start of get_user or at a setUp() function:
    User.objects.create(email='[email protected]', password='somepass'....) (populate the rest) so you can query it later
  3. Use asserts to write your tests, you shouldn't use print, here's an example:
class UserModelTest(unittest.TestCase):
    def test_get_user_and_data_is_serialized(self):
        User.objects.create(email='[email protected]', password='somepass'....)
        user = User.objects.all()[0]  # start testing one entry.
        self.assertEqual(user.email, '[email protected]', 'Incorret email') # message when test fails
        serialized_user = UserSerializer(users,many=False)
        self.assertEqual(userserializer.data, {'email': '[email protected]', ....}, 'oops the data is not serialized')

Just change the data you are testing against, this is just an example

There is much more to know about unittest module, Read this if you need more help https://docs.python.org/2/library/unittest.html

Also check this book by Harry J.W.Percival, one great resource:
http://www.obeythetestinggoat.com/

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

very good explanation,I will dive deep into it :) Thanks very much!
i recommend you read this book, i'm currently following it up, best you can find out there obeythetestinggoat.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.