Automatically run django unittests - django

I want to create a script to automatically run the tests in django. So I want to perform the equivalent of python manage.py test myapp within python and store wether the test failed or not as a variable.
So if the test works:
variable = True
if the test doesn't work:
variable = False

You can do this with your own test runner using suite_result. Related code here

Related

Django unit tests fail when ran as a whole and there is GET call to the API

I am facing an issue when I run the tests of my django app with the command
python manage.py test app_name OR
python manage.py test
All the test cases where I am fetching some data by calling the GET API, they seem to fail because there is no data in the response in spite of there being in the test data. The structure which I have followed in my test suite is there is a base class of django rest framework's APITestCase and a set_up method which creates test objects of different models used in the APIs and I inherit this class in my app's test_views class for any particular API
such as
class BaseTest(APITestCase):
def set_up(self):
'''
create the test objects which can be accessed by the main test
class.
'''
self.person1= Person.objects.create(.......)
class SomeViewTestCase(BaseTest):
def setUp(self):
self.set_up()
def test_some_api(self):
url='/xyz/'
self.client.login(username='testusername3',password='testpassword3')
response=self.client.get(url,{'person_id':self.person3.id})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data),6)
So whenever I run the test as
./manage.py test abc.tests.test_views.SomeViewTestCase
it works fine, but when I run as
./manage.py test abc
The test above response.data has 0 entries and similarly, with the other tests within the same class the data is just not fetched and hence all the asserts fail.
How can I ensure the successful run of the test when they are run as a whole because during deployment they have to go through CI?
The versions of the packages and system configuration are as follows:
Django Version -1.6
Django Rest Framework - 3.1.1
Python -2.7
Operating System - Mac OS(Sierra)
Appreciate the help.Thanks.
Your test methods are executed in arbitrary order... And after each test, there's a tearDown() method that takes care to "rollback to initial state" so you have isolation between tests execution.
The only part that is shared among them is your setUp() method. that is invoked each time a test runs.
This means that if the runner start from the second test method and you only declare your response.data in your first test, all tests are gonna fail apart the posted one.
Hope it helps...

How can i run a single odoo test file with PyDev unittest in eclipse?

I seted the config data like db_name in setUp()
def setUp(self):
config = tools.config
config['db_name'] = 'test'
config['db_user'] = 'admin'
...
and got a "AttributeError: environments" for "return cls._local.environments" in setUp() super
I must say I haven't really used odoo to know what it needs configured, but apparently there's a pytest plugin for it:
https://pypi.python.org/pypi/pytest-odoo
So, my suggestion would be to try to use pytest instead of unittest.TestCase along with that plugin (which should take care of making the proper setup) -- the only thing in PyDev in this case is ask it to use the pytest runner (see: http://www.pydev.org/manual_adv_pyunit.html for details on how to configure that).

ODOO [V8] Unit Tests

I'm actually trying to run the unittests I've created thanks to Odoo's documentation.
I've built my module like this :
module_test
- __init__.py
__openerp.py__
...
- tests
__init__.py
test_1.py
Inside 'module_test/tests/init.py', I do have "import test_1"
Inside, 'module_test/tests/test_1.py", I do have : "import tests + a test scenario I've written.
Then I launch the command line to run server, and I add :
'-u module_test --log-level=test --test-enable' to update the module and activate the tests run
The shell returns : "All post-tested in 0.00s, 0 queries".
So in fact, no tests are run.
I then added a syntax error, so the file can't be compiled by the server, but shell returned the same sentence. It looks like the file is ignored, and the server is not even trying to compile my file... I do not understand why ?
I've checked some Odoo source module, the 'sale' one for example.
I've tried to run sale tests, shell returned the same value than before.
I added syntax error inside sale tests, shell returned the same value again, and again.
Does anyone have an idea about this unexpected behavior ?
You should try using post_install decorator for test class:
Example:
from openerp.tests import common
#common.post_install(True)
class TestPost(common.TransactionCase):
def test_post_method(self):
response = self.env['my_module.my_model'].create_post('hello')
self.assertEqual(response['success'], True)
To make the tests perform faster without updating your module, you should be able to run tests without
-u module_test
if you use
--load=module_test
I have to admit that odoo testing documentation is really bad. It took me a week to figure out how to make unit testing work in odoo.

Command for karma-jasmine to stop unit-test after first fail

Is there any command for karma-jasmine unit-test to stop the test when it encounters the first test fail. For example, in python the command is like:
py.test -x # stop after first failure
py.test --maxfail=2 # stop after two failures
Currently I am using node_modules/karma/bin/karma start that run all the tests and stops only after everything is executed
This would require creating a custom reporter, or changing the reporter in the karma-jasmine adapter to stop on spec failure as such:
this.specDone = function (specResult)
{
var failure = specResult.failedExpectations.length;
if (failure)
{
suiteDone();
jasmineDone();
}
}
References
jasmine.io: custom_reporter.js
karma-jasmine source: adapter.js
Jasmine Issue #842: Async reporter hooks
Protractor Issue #1938: Find a good pattern for waiting for Jasmine Reporters
Alternatively you can just tell jasmine you want to run a specific Spec or Specs in a folder so you only are testing a subset of your tests and not running all in your suite.

Django testrunner for both Celery and Selenium

Django-celery expects me to set
TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'
and django-selenium expects me to set
TEST_RUNNER = 'django_selenium.selenium_runner.SeleniumTestRunner'.
How can I have both, i.e., both tests that run celery tasks locally and tests that use selenium to control a browser?
you could probably define your own test runner that inherits from them both
(looking at the source for the two, the celery one actually just sets some settings)
so make some file e.g. myapp.test_runner, with
from djcelery.contrib.test_runner import CeleryTestSuiteRunner
django_selenium.selenium_runner import SeleniumTestRunner
class MyRunner(CeleryTestSuiteRunner, SeleniumTestRunner):
pass
and then set
TEST_RUNNER = 'myproject.myapp.test_runner.MyRunner'