Reputation: 829
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!
Upvotes: 0
Views: 337
Reputation: 2103
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:
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.get_user
or at a setUp()
function:User.objects.create(email='[email protected]', password='somepass'....)
(populate the rest) so you can query it laterclass 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!
Upvotes: 3