Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:19:35+00:00 2026-06-12T00:19:35+00:00

I want to better write this test: def test_profile_created(self): self.client.post(reverse(‘registration_register’), data={ ‘username’:’ygam’, ’email’:’ygam@example.com’, ‘password1′:’ygam’,

  • 0

I want to better write this test:

def test_profile_created(self):
        self.client.post(reverse('registration_register'), data={
            'username':'ygam',
            'email':'ygam@example.com',
            'password1':'ygam',
            'password2':'ygam'
        })
        """
        Test if a profile is created on save
        """
        user = User.objects.get(username='ygam')
        self.assertTrue(UserProfile.objects.filter(user=user).exists())

and I just came upon this code on django-registration tests that does not actually “create” the user:

def test_registration_signal(self):
        def receiver(sender, **kwargs):
            self.failUnless('user' in kwargs)
            self.assertEqual(kwargs['user'].username, 'bob')
            self.failUnless('request' in kwargs)
            self.failUnless(isinstance(kwargs['request'], WSGIRequest))
            received_signals.append(kwargs.get('signal'))

        received_signals = []
        signals.user_registered.connect(receiver, sender=self.backend.__class__)

        self.backend.register(_mock_request(),
                              username='bob',
                              email='bob@example.com',
                              password1='secret')

        self.assertEqual(len(received_signals), 1)
        self.assertEqual(received_signals, [signals.user_registered])

However he used a custom function for this “_mock_request”:

class _MockRequestClient(Client):
    def request(self, **request):
        environ = {
            'HTTP_COOKIE': self.cookies,
            'PATH_INFO': '/',
            'QUERY_STRING': '',
            'REMOTE_ADDR': '127.0.0.1',
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': '',
            'SERVER_NAME': 'testserver',
            'SERVER_PORT': '80',
            'SERVER_PROTOCOL': 'HTTP/1.1',
            'wsgi.version': (1,0),
            'wsgi.url_scheme': 'http',
            'wsgi.errors': self.errors,
            'wsgi.multiprocess':True,
            'wsgi.multithread': False,
            'wsgi.run_once': False,
            'wsgi.input': None,
            }
        environ.update(self.defaults)
        environ.update(request)
        request = WSGIRequest(environ)

        # We have to manually add a session since we'll be bypassing
        # the middleware chain.
        session_middleware = SessionMiddleware()
        session_middleware.process_request(request)
        return request


def _mock_request():
    return _MockRequestClient().request()

However, it may be too long of a function for my needs. I want to be able to somehow “fake” the account creation. I have not much experience on mocks and stubs so any help would do. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:19:37+00:00Added an answer on June 12, 2026 at 12:19 am

    These two approaches are testing different things:

    1. Your test case tests the model layer — it asks the question “if I create a user and save it, does the profile record get created automatically?”

      It doesn’t depend on how the user object was created — Through an admin view, or a user signup view, or a management command — as long as this test passes, you know that any method of creating a user through the ORM will add a profile.

      It also doesn’t care how the profile is created. It could be through a pre-save signal, a post-save signal, some code in an overridden save() method, or magic. As long as the record is created, the test will pass.

    2. The code you found in django-registration is testing the view layer. It is calling a specific view function, register, passing it three URL parameters, and asking the question “did the user_registered signal get fired as a result of this view?”

      This test case doesn’t look at other methods of creating a user; just the registration view. It’s not concerned with any other methods of creating a user.

      It also doesn’t look at whether a profile was created. All it wants to know is that the signal was fired, regardless of what that signal actually does in any particular application.

    What’s the difference?

    Your code is a great example of an integration test — “Did I put all of these parts (models, signals) together in right order so that profiles get created?” — and of a regression test, to signal you in the future whether you’ve done anything that has broken the user profile creation.

    The django-registration code is an actual unit test. It very carefully isolates just the view code, to call it in a controlled environment and ask one simple question of it.

    Which is better?

    In your case, you probably don’t need to write the unit test. (Though there could be a lot of debate about that; it’s practically a matter of religion in some parts) You want a way to verify that the code that pieces that you’ve put together are working as you expect them to, and your test is the best way to do that.

    Don’t worry about actually creating a user — during the test run, you’re doing everything in a transaction, and that transaction is rolled back immediately after the test method completes. It may not even hit the disk. And, as @Platinum Azure mentioned, it’s all on a test database anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't want to make this into a which is better... MVC or WebForms
I want to test some QAbstractListModels I already implemented, is it better to use
I want to be better at using NUnit for testing the applications I write,
I want to better understand the capabilities and limitations of what can be done
what is the best free/open source WinForms progress bar control? (i.e. want a better
I have a .exe app which I want to understand better - I can
See here http://www.hanselman.com/blog/InSearchOfThePerfectMonospacedProgrammersFontInconsolata.aspx - for want of a better description - the statement block
I want to know that is there any better method to do : var
I want to know if there is a better way (than what I'm currently
I want to use some NSAssert stuff and other things to enable better debugging

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.