python - How Can You Create an Admin User with Factory_Boy? -


i'm relative django beginner , started doing testing projects. want build functional test selenium logs django admin site.

i first followed tutorial http://www.tdd-django-tutorial.com/tutorial/1/ , used fixtures , dumpdata make admin account info available testing app (which creates new database). works fine.

i wanted see if can same using factory-boy replace fixtures. factory boy works instantiating necessary object within tests.py file seems cleaner me. somehow can't work , factory_boy documentation not helpful...

here tests.py

from django.test import liveservertestcase django.contrib.auth.models import user selenium import webdriver selenium.webdriver.common.keys import keys import factory  class userfactory(factory.factory):     factory_for = user      username = 'jeff'     password = 'pass'     is_superuser = true  class admintest(liveservertestcase):      def setup(self):         self.browser = webdriver.firefox()      def teardown(self):         self.browser.quit()      def test_if_admin_login_is_possible(self):         jeff = userfactory.create()          # jeff opens browser , goes admin page         self.browser = webdriver.firefox()         self.browser.get(self.live_server_url + '/admin/')          # jeff sees familiar 'django administration' heading         body = self.browser.find_element_by_tag_name('body')         self.assertin('django administration', body.text)          # jeff types in username , password , hits return         username_field = self.browser.find_element_by_name('username')         username_field.send_keys(jeff.username)         password_field = self.browser.find_element_by_name('password')         password_field.send_keys(jeff.password)         password_field.send_keys(keys.return)          # jeff finds himself on 'site administration' page         body = self.browser.find_element_by_tag_name('body')         self.assertin('site administration', body.text)          self.fail('fail...') 

this fails log in somehow doesn't create valid admin account. how can using factory-boy? possible or need use fixtures that?

(in post people suggested fixtures necessary factory boy didn't come up: how create admin user in django tests.py. tried solution suggested @ bottom in same answer: https://stackoverflow.com/a/3495219/1539688. didn't work me...)

if subclass factory.djangomodelfactory should save user object you. see note section under postgenerationmethodcall. need following:

class userfactory(factory.djangomodelfactory):     factory_for = user      email = 'admin@admin.com'     username = 'admin'     password = factory.postgenerationmethodcall('set_password', 'adm1n')      is_superuser = true     is_staff = true     is_active = true 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -