<commit_before>Usage
=====

Web browser automation
----------------------

You can access the test HTTP server from your preferred web automation
library via ``context.base_url`` (normally, this would be set to
``http://localhost:8081``).  Alternatively, you can use
``context.get_url()``, which is a helper function for absolute paths and
reversing URLs in your Django project.  It takes an absolute path, a view
name, or a model as an argument, similar to `django.shortcuts.redirect`_.

Examples:

.. code-block:: python

    # Using Splinter
    @when(u'I visit "{url}"')
    def visit(context, url):
        context.browser.visit(context.base_url + url)

.. code-block:: python

    # Get context.base_url
    context.get_url()
    # Get context.base_url + '/absolute/url/here'
    context.get_url('/absolute/url/here')
    # Get context.base_url + reverse('view-name')
    context.get_url('view-name')
    # Get context.base_url + reverse('view-name', 'with args', and='kwargs')
    context.get_url('view-name', 'with args', and='kwargs')
    # Get context.base_url + model_instance.get_absolute_url()
    context.get_url(model_instance)

Django’s testing client
-----------------------

Attached to the context is an instance of TestCase.  You can access it
via ``context.test``.  This means you can do things like use Django’s
testing client.

.. code-block:: python

    # Using Django's testing client
    @when(u'I visit "{url}"')
    def visit(context, url):
        context.response = context.test.client.get(url) # save response in context for next step

unittest + Django assert library
--------------------------------

Additionally, you can utilize unittest and Django’s assert library.

.. code-block:: python

    @when(u'I should see "{text}"')
    def visit(context, text):
        response = context.response # from previous step
        context.test.assertContains(response, text)

Database transactions per scenario
----------------------------------

Each scenario is run inside a database transaction, just like your
regular TestCases.  So you can do something like:

.. code-block:: python

    @given(u'user "{username}" exists')
    def create_user(context, username):
        # This won't be here for the next scenario
        User.objects.create_user(username=username, password='<PASSWORD>')

And you don’t have to clean the database yourself.<commit_msg>put the comments above the code<commit_after>Usage
=====

Web browser automation
----------------------

You can access the test HTTP server from your preferred web automation
library via ``context.base_url`` (normally, this would be set to
``http://localhost:8081``).  Alternatively, you can use
``context.get_url()``, which is a helper function for absolute paths and
reversing URLs in your Django project.  It takes an absolute path, a view
name, or a model as an argument, similar to `django.shortcuts.redirect`_.

Examples:

.. code-block:: python

    # Using Splinter
    @when(u'I visit "{url}"')
    def visit(context, url):
        context.browser.visit(context.base_url + url)

.. code-block:: python

    # Get context.base_url
    context.get_url()
    # Get context.base_url + '/absolute/url/here'
    context.get_url('/absolute/url/here')
    # Get context.base_url + reverse('view-name')
    context.get_url('view-name')
    # Get context.base_url + reverse('view-name', 'with args', and='kwargs')
    context.get_url('view-name', 'with args', and='kwargs')
    # Get context.base_url + model_instance.get_absolute_url()
    context.get_url(model_instance)

Django’s testing client
-----------------------

Attached to the context is an instance of TestCase.  You can access it
via ``context.test``.  This means you can do things like use Django’s
testing client.

.. code-block:: python

    # Using Django's testing client
    @when(u'I visit "{url}"')
    def visit(context, url):
        # save response in context for next step
        context.response = context.test.client.get(url)

unittest + Django assert library
--------------------------------

Additionally, you can utilize unittest and Django’s assert library.

.. code-block:: python

    @when(u'I should see "{text}"')
    def visit(context, text):
        # from previous step
        response = context.response
        context.test.assertContains(response, text)

Database transactions per scenario
----------------------------------

Each scenario is run inside a database transaction, just like your
regular TestCases.  So you can do something like:

.. code-block:: python

    @given(u'user "{username}" exists')
    def create_user(context, username):
        # This won't be here for the next scenario
        User.objects.create_user(username=username, password='<PASSWORD>')

And you don’t have to clean the database yourself.
{'fraction_non_alphanumeric': 0.1502288805659592, 'fraction_numerical': 0.0016645859342488557, 'mean_word_length': 3.019230769230769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453427', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1314, 'n_words': 468}