Unit Testing with django-pipeline
django ci/cd pipeline
I have problems unit testing views of an application that uses django-pipeline? Whenever I perform a client.get() on any URL, it produces the following exception:
ValueError: The file 'css/bootstrap.css' could not be found with <pipeline.storage.PipelineCachedStorage object at 0x10d544950>.
The fact that it is bootstrap.css is of course not important, but that I'm unable to execute view rendering due to this exception.
Any guide / tips are welcome!
I ran into a similar problem. However, setting
when running the tests only partly solved my issue. I also had to disable the pipeline completely if you want to run LiverServerTestCase tests without having to calling 'collecstatic' before running the tests:
Since django 1.4 it's fairly easy to modify settings for tests - there is a handy decorator that works for methods or TestCase classes:
from django.test.utils import override_settings @override_settings(STATICFILES_STORAGE='pipeline.storage.NonPackagingPipelineStorage', PIPELINE_ENABLED=False) class BaseTestCase(LiveServerTestCase): """ A base test case for Selenium """ def setUp(self): ...
However this produced inconsistent results as @jrothenbuhler describes in his answer. Regardless, this is less than ideal if you are running integration tests - you should mimic production as much as possible to catch any potential issues. It appears django 1.7 has a solution for this in the form of a new test case "StaticLiveServerTestCase". From the docs: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerCase
This unittest TestCase subclass extends django.test.LiveServerTestCase.
Just like its parent, you can use it to write tests that involve running the code under test and consuming it with testing tools through HTTP (e.g. Selenium, PhantomJS, etc.), because of which it’s needed that the static assets are also published.
I haven't tested this, but sounds promising. For now I'm doing what @jrothenbuhler in his solution using a custom test runner, which doesn't require you to run collectstatic. If you really, really wanted it to run collectstatic you could do something like this:
from django.conf import settings from django.test.simple import DjangoTestSuiteRunner from django.core.management import call_command class CustomTestRunner(DjangoTestSuiteRunner): """ Custom test runner to get around pipeline and static file issues """ def setup_test_environment(self): super(CustomTestRunner, self).setup_test_environment() settings.STATICFILES_STORAGE = 'pipeline.storage.NonPackagingPipelineStorage' call_command('collectstatic', interactive=False)
TEST_RUNNER = 'path.to.CustomTestRunner'
Unit Testing with django-pipeline - django - iOS, I have problems unit testing views of an application that uses django-pipeline? Whenever I perform a client.get() on any URL, it produces the following These tests may include UI testing, load testing, integration testing, API reliability testing, etc. This helps developers more thoroughly validate updates and pre-emptively discover issues. With the cloud, it is easy and cost-effective to automate the creation and replication of multiple environments for testing, which was previously difficult
I've been running into the same problem. I tackled it using a custom test runner:
from django.conf import settings from django.test.simple import DjangoTestSuiteRunner from pipeline.conf import settings as pipeline_settings class PipelineOverrideRunner(DjangoTestSuiteRunner): def setup_test_environment(self): '''Override STATICFILES_STORAGE and pipeline DEBUG.''' super(PipelineOverrideRunner, self).setup_test_environment() settings.STATICFILES_STORAGE = 'pipeline.storage.PipelineFinderStorage' pipeline_settings.DEBUG = True
Then in your settings.py:
TEST_RUNNER = 'path.to.PipelineOverrideRunner'
Setting pipeline's DEBUG setting to True ensures that the static files are not packaged. This prevents the need to run collectstatic before running the tests. Note that it's pipeline's DEBUG setting, not Django's, which is overridden here. The reason for this is that you want Django's DEBUG to be False when testing to best simulate the production environment.
Setting STATICFILES_STORAGE to PipelineFinderStorage makes it so that the static files are found when Django's DEBUG setting is set to False, as it is when running tests.
The reason I decided to override these settings in a custom test runner instead of in a custom TestCase is because certain things, such as the django.contrib.staticfiles.storage.staticfiles_storage object, get set up once based on these and other settings. When using a custom TestCase, I was running into problems where tests would pass and fail inconsistently depending on whether the override happened to be in effect when modules such as django.contrib.staticfiles.storage were loaded.
Django continuous integration, Pipeline is an asset packaging library for Django. Make unit tests run against Django 1.6 and 1.7. Thanks to Sławek Ehlert. The simplest unit test usually includes three distinct steps: Arrange, Act and Assert. Arrange: Set up the any variables and objects necessary. Act: Call the method being tested, passing any parameters needed; Assert: Verify expected results; The unit test project should have a dependency for the app project that it’s testing.
I ran into the same problem. I managed to get around it by using a different
STATIC_FILES_STORAGE when I'm testing:
STATICFILES_STORAGE = 'pipeline.storage.NonPackagingPipelineStorage'
I have separate settings files for production and testing, so I just put it in my test version, but if you don't, you could probably wrap it in
It took a little more effort, because this can only present during the unittesting. To address that, I used the snippet at http://djangosnippets.org/snippets/1011/ and created a UITestCase class:
class UITestCase(SettingsTestCase): ''' UITestCase handles setting the Pipeline settings correctly. ''' def __init__(self, *args, **kwargs): super(UITestCase, self).__init__(*args, **kwargs) def setUp(self): self.settings_manager.set( STATICFILES_STORAGE='pipeline.storage.NonPackagingPipelineStorage')
Now all of my tests that need to render UI that incude compressed_css tags use UITestCase instead of django.test.TestCase.
I ran into the same problem, and it turned out to be because I had
TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'
I don't understand how, but it must somehow have interacted with Pipeline. Once I removed that setting, the problem went away.
I still needed to force Celery to be eager during testing, so I used
override_settings for the tests that needed it:
from django.test.utils import override_settings … class RegisterTestCase(TestCase): @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_ALWAYS_EAGER=True, BROKER_BACKEND='memory') def test_new(self): …
jazzband/django-pipeline: Pipeline is an asset packaging , Creating tests for the app. Our CI pipeline needs to have tests so that we can ensure that our automated build is A-Okay before any new commit is Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great
Same here. Refers to this issues: https://github.com/cyberdelia/django-pipeline/issues/277
As I use py.test, I put this in conftest.py as a workaround:
import pytest from django.conf import settings def pytest_configure(): # workaround to avoid django pipeline issue # refers to settings.STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
Continuous integration for Django projects, Productive unit testing is a big topic, and an almost essential part of a development process Duration: 17:02 Posted: May 9, 2019 To assert or assertEqual in Python unit testing. When you write unit tests in Python you can use these widgets: Find static files defined in django-pipeline
Basic Unit Testing in Django, Testing Django applications with Bitbucket Pipelines and MySQL evident when running my integration tests immediately after my unit tests. A unit test checks a small component in your application. You can write both integration tests and unit tests in Python. To write a unit test for the built-in function sum(), you would check the output of sum() against a known output. For example, here’s how you check that the sum() of the numbers (1, 2, 3) equals 6: >>> >>>
Testing Django applications with Bitbucket Pipelines and MySQL , A demo CI/CD pipeline using Python Django and Pytest when I started I found a problem: there was not CI/CD working or integration/unit test. Test and deploy a Python application with GitLab CI/CD. This example will guide you how to run tests in your Python application and deploy it automatically as Heroku application. You can also view or fork the complete example source. Configure project. This is what the .gitlab-ci.yml file looks like for this project:
Python, Django and GitHub CI/CD - Gabi Cavalcante, To cut things short continuous integration is the build and unit testing AWS code pipeline is a fully managed CI delivery service that helps you In SDLC, STLC, V Model, Unit testing is first level of testing done before integration testing. Unit testing is a WhiteBox testing technique that is usually performed by the developer. Though, in a practical world due to time crunch or reluctance of developers to tests, QA engineers also do unit testing.
- Is DEBUG set to True or False ?
- can you test the view directly instead of using the django test client? Maybe using mocks for checking on template rendering + context?
- Jonas, DEBUG is set to True, which should bypass the pipeline action. (I will double-check this though.) Thanks!
- Hwjp, as I gave up trying to figure out a solution, I ended up leaving the view untested, and simply test the other classes.
- The default Django test runner sets DEBUG to False, no matter what is in your settings file: Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False. This is to ensure that the observed output of your code matches what will be seen in a production setting.