Test not found in github actions - django

I am trying to do an automated test.
There should be 21 tests, but github-actions can't find them for some reason.
https://github.com/duri0214/portfolio/actions/runs/4215160033/jobs/7316095166#step:3:6
manage.py is under mysite directory, so...
(Below is when I run it on my local PC)
(venv) PS D:\OneDrive\dev\portfolio\mysite> python manage.py test
Found 21 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
:
self.client.post(reverse('vnm:likes', kwargs={'user_id': 1, 'article_id': 99}), follow=True)
AssertionError: ObjectDoesNotExist not raised
======================================================================
FAIL: test_post_click_good_button
(vietnam_research.tests.test_views.TestView)
----------------------------------------------------------------------
OK
Anyone know a solution?
thanks

Related

Testing Django Rest Framework POST returns 500 despite call working

Update
This issue was caused by me not including a token in the APIClient's header. This is resolved.
I have a standard ModelViewSet at /test-endpoint. I am trying to use APIClient to test the endpoint.
from rest_framework.test import APIClient
... # During this process, a file is uploaded to S3. Could this cause the issue? Again, no errors are thrown. I just get a 500.
self.client = APIClient()
...
sample_call = {
"name": "test_document",
"description": "test_document_description"
}
response = self.client.post('/test-endpoint', sample_call, format='json')
self.assertEqual(response.status_code, 201)
This call works with the parameters I set in sample_call. It returns a 201. When I run the test, however, I get a 500. How can I modify this to get the 201 passed?
I run the tests with python src/manage.py test modulename
To rule out the obvious, I copy-pasted the sample call into Postman and run it without issue. I believe the 500 status code is coming from the fact that I'm testing the call and not using it in a live environment.
No error messages are being thrown beyond the AssertionError:
AssertionError: 500 != 201
Full Output of testing
/home/bryant/.virtualenvs/REDACTED/lib/python3.4/site- packages/django_boto/s3/shortcuts.py:28: RemovedInDjango110Warning: Backwards compatibility for storage backends without support for the `max_length` argument in Storage.get_available_name() will be removed in Django 1.10.
s3.save(full_path, fl)
F
======================================================================
FAIL: test_create (sample.tests.SampleTestCase)
Test CREATE Document
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/bryant/api/redacted/src/sample/tests.py", line 31, in test_create
self.assertEqual(response.status_code, 201)
AssertionError: 500 != 201
----------------------------------------------------------------------
Ran 1 test in 2.673s
FAILED (failures=1)
Destroying test database for alias 'default'...
The S3 warning is expected. Otherwise, all appears normal.
To debug a failing test case in Django/DRF:
Put import pdb; pdb.set_trace() just before the assertion and see the request.content as suggested by #Igonato, or you can just add a print(request.content), there is no shame for it.
Increase verbosity of your tests by adding -v 3
Use dot notation to investigate the specific test case: python src/manage.py test modulename.tests.<TestCase>.<function>
I hope these are useful to keep in mind.

AttributeError: TestSwitch instance has no attribute 'assertTrue'

I have a following pyunit test case code where I am collecting the result of the function (True or False) and using it to drive my assertion. However, I am getting the "no attribute" error for assertTrue. What is missing here?
I am using python 2.7.8 and pyunit version of PyUnit-1.4.1-py2.7.
The same code when run from the Eclipse (pydev plugin) from my Mac, it works fine. Only when I take this to my Linux box, it does throw below error. So to me it looks like some package incompatibility problem.
import json
import unittest
class TestSwitch(unittest.TestCase):
def testFunction(self):
self.assertTrue(True, "test case failed")
Below is the test suite class.
import unittest
from mysample import TestSwitch
# Create an instance of each test case.
testCase = TestSwitch('testFunction')
# Add test cases to the test suite.
testSuite = unittest.TestSuite()
testSuite.addTest(testCase)
# Execute the test suite.
testRunner = unittest.TextTestRunner(verbosity=2)
testRunner.run(testSuite)
It throws below error.
bash-3.2$ python mysuite.py
testFunction (mysample.TestSwitch) ... ERROR
======================================================================
ERROR: testFunction (mysample.TestSwitch)
----------------------------------------------------------------------
Traceback (most recent call last):
File "workspace/pyunit/mysample.py", line 7, in testFunction
self.assertTrue(True, "test case failed")
AttributeError: TestSwitch instance has no attribute 'assertTrue'
----------------------------------------------------------------------
Ran 1 tests in 0.000s
FAILED (errors=1)
bash-3.2$
For now I've figured a workaround for this problem by using 'assertEqual' comparing with a boolean value and it works. I am not sure why 'assertTrue' and for that matter 'assertFalse' is having problem. I did not change any package version or anything.
The workaround code is as below.
17 def testFunction(self):
18 res = True
19 self.assertEqual(res, True, 'test case failed')

2 arguments missing but method works

Could you help me understand what is going on here. The question is about the error in the traceback. The failure is just as the illustration. And what I would like to illustrate that the function works.
Well, I was told that 2 positional arguments: 'view_instance' and 'address' are missing.
But the method really has taken those 2 positional arguments and worked happily till its logical end. In the interactive playing I show that I can catch the arguments transmitted.
Why does error appear? Thank you in advance for your help.
ADDED LATER:
Well, this seems to be because of the 'test_' beginning of the function.
Without "test" it works (def anonymous_user_redirected_to_login_page(self, view_instance, address):).
/photoarchive/general/tests.py
class GeneralTest(TestCase):
def test_anonymous_user_redirected_to_login_page(self, view_instance, address):
pdb.set_trace()
request = RequestFactory().get(address)
request.user = AnonymousUser()
response = view_instance(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/accounts/login/')
def test_anonymous_user_from_home_page_redirected_to_login_page(self):
view_instance = HomePageView.as_view()
address = '/'
self.test_anonymous_user_redirected_to_login_page(view_instance, address)
Traceback
(photoarchive) michael#michael:~/workspace/photoarchive/photoarchive$ python manage.py test general
Creating test database for alias 'default'...
FE
======================================================================
ERROR: test_anonymous_user_redirected_to_login_page (general.tests.GeneralTest)
----------------------------------------------------------------------
TypeError: test_anonymous_user_redirected_to_login_page() missing 2 required positional arguments: 'view_instance' and 'address'
======================================================================
FAIL: test_anonymous_user_from_home_page_redirected_to_login_page (general.tests.GeneralTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 29, in test_anonymous_user_from_home_page_redirected_to_login_page
self.test_anonymous_user_redirected_to_login_page(view_instance, address)
File "/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 23, in test_anonymous_user_redirected_to_login_page
self.assertEqual(response.status_code, 302)
AssertionError: 200 != 302
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...
Interactive playing:
(photoarchive) michael#michael:~/workspace/photoarchive/photoarchive$ python manage.py test general
Creating test database for alias 'default'...
> /home/michael/workspace/photoarchive/photoarchive/general/tests.py(20)test_anonymous_user_redirected_to_login_page()
-> request = RequestFactory().get(address)
(Pdb) view_instance
<function HomePageView at 0x7faa0f76fea0>
(Pdb) address
'/'
(Pdb)
test_anonymous_user_redirected_to_login_page() method is treated by unittest framework as a test method, because its name starts with test. The framework tries to execute it, but is not passing any arguments to it (test methods don't normally take any arguments). However, the method requires them, hence the error.
If this method is only a helper method to be called from the other method, name it so that it doesn't start with test, e.g. _test_anonymous_user_redirected_to_login_page().
Note that the traceback is not related to this problem. The traceback simply shows where the other test method failed at an assertion. That is, the other test method runs correctly (both in unittest run and in your interactive session).

Django Testing: DatabaseCreation' object has no attribute '_rollback_works'

I have written a example test and I'm trying to run it without creating a new database every time.
The first time I run my test everything is ok (takes sometime due to building the DB):
> REUSE_DB=1 python manage.py test contacts
Ran 1 test in 0.251s
The second time I get the following error:
> REUSE_DB=1 python manage.py test contacts
nosetests --verbosity 1 contacts
AttributeError: 'DatabaseCreation' object has no attribute '_rollback_works'
Why and how do I solve? Thanks.
My Test:
class ExampleTestCase(TestCase):
def test_contact_page(self):
resp = self.client.get('/contact/single/')
self.assertEqual(resp.status_code, 200)
Settings.py
DEBUG = True
TEMPLATE_DEBUG = DEBUG
INSTALLED_APPS += (
'django_nose',
)
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
Just use nose from github and you're good to go! I belive this is your issue:
https://github.com/jbalogh/django-nose/pull/95
I came across this a long while ago, it is now fixed on the github master, but unfortunately django-nose is not updated on pypi since last year..

Testing reusable Django apps with Django testrunner

I want to test a small reusable app which comes with its own settings module. Global (project) settings are accessed inside app's settings to support variables' overriding, e.g.
# in <my_app>/settings.py
from django.conf import settings
MY_SETTING_VAR = getattr(settings, 'MY_OVERRIDDEN_VAR', False)
When I run tests with manage.py test myapp I get the following:
ImportError: Settings cannot be imported, because environment
variable DJANGO_SETTINGS_MODULE is undefined.
What is the right way to run tests in this case?
I'm not sure, i tested exactly what you posted and it works for me:
<<< 12:18.25 Fri Feb 24 2012!~/testproject
<<< jpic#germaine!10019 env
>>> ./manage.py test testapp
Creating test database for alias 'default'...
Destroying old test database 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Destroying test database for alias 'default'...
<<< 12:18.27 Fri Feb 24 2012!~/testproject
<<< jpic#germaine!10020 env
>>> cat testapp/tests.py
from django.test import TestCase
from .settings import *
class SomeTestCase(TestCase):
def testSomething(self):
self.assertEqual(MY_SETTING_VAR, 'default')
<<< 12:18.30 Fri Feb 24 2012!~/testproject
<<< jpic#germaine!10021 env
>>> cat testapp/settings.py
from django.conf import settings
MY_SETTING_VAR = getattr(settings, 'MY_OVERRIDDEN_VAR', 'default')
You want to make sure your actual code matches this working code.
It's better for an app to include a dummy project that demonstrates the app or at least allows testing. For example:
<<< 12:42.56 Fri Feb 24 2012!~/testproject/testapp
<<< jpic#germaine!10034 E:1 env
>>> pip install -e git+git#github.com:subsume/django-subscription.git#egg=sub
Obtaining sub from git+git#github.com:subsume/django-subscription.git#egg=sub
Cloning git#github.com:subsume/django-subscription.git to /home/jpic/env/src/sub
Running setup.py egg_info for package sub
Installing collected packages: sub
Running setup.py develop for sub
Creating /home/jpic/env/lib/python2.7/site-packages/django-subscription.egg-link (link to .)
Removing django-subscription 0.0 from easy-install.pth file
Adding django-subscription 0.1 to easy-install.pth file
Installed /home/jpic/env/src/sub
Successfully installed sub
Cleaning up...
<<< 12:43.08 Fri Feb 24 2012!~/testproject/testapp
<<< jpic#germaine!10035 env
<<< 12:43.11 Fri Feb 24 2012!~/testproject/testapp
<<< jpic#germaine!10035 env
>>> cd ../../env/src/sub
<<< 12:43.15 Fri Feb 24 2012!~/env/src/sub
<<< jpic#germaine!10036 G:master env
>>> ls
django_subscription.egg-info docs README setup.py subscription subscription_test_project
<<< 12:43.16 Fri Feb 24 2012!~/env/src/sub
<<< jpic#germaine!10037 G:master env
>>> cd subscription_test_project
<<< 12:43.20 Fri Feb 24 2012!~/env/src/sub/subscription_test_project
<<< jpic#germaine!10038 G:master env
>>> ./manage.py test subscription
Creating test database for alias 'default'...
........
----------------------------------------------------------------------
Ran 8 tests in 0.012s
OK
Destroying test database for alias 'default'...
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
You get this because DJANGO_SETTINGS_MODULE is not in your python environment variables... To solve your problem you must define it as
import os
os.environ['DJANGO_SETTINGS_MODULE'] = '<django_application_root>.settings'
You can add it to your root __init__.py file...