Volatil3
Volatil3

Reputation: 14988

Import error while running specific test case

Before I say something, let me show the current project hierarchy. The app name is api.

── api
│   ├── 
│   ├── admin.py
│   ├── apps.py
│   ├── init.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_wallet_customer_id.py
│   │   ├── 0003_auto_20201201_1103.py
│   │   ├── 0004_auto_20201203_0703.py
│   │   ├── 0005_auto_20201207_1355.py
│   │   ├── __init__.py
│   │ 
│   ├── models.py
│   ├── permissions.py
│   ├── serializers.py
│   ├── stellar.py
│   ├── te_s_ts_bk.py
│   ├── te_sts_2.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── debug.log
├── manage.py
└── myapp
    ├── __init__.py    
    ├── asgi.py
    ├── settings-local.py
    ├── settings-remote.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

As it was suggested here, I even removed __init__.py file from the api app folder, yet having the issue.

My TestCase Class look like below:

class CreateWalletTestCase(APITestCase):
    app_id = 0
    app = None

    def test_create_wallet(self):
        response = self.client.post(
            '/wallets/',
            data={'app_id': self.app_id, 'customer_id': 'A-001'},
            **{'HTTP_api_key': 'test-api', 'HTTP_api_secret': 'test-api-password'}
        )
        data = json.loads(response.content)
        self.assertEqual(data['status'], 'OK

When I run as python manage.py test api.CreateWalletTestCase.test_create_wallet or even python manage.py test CreateWalletTestCase.test_create_wallet, it generates the error:

ImportError: Failed to import test module: CreateWalletTestCase
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'api.CreateWalletTestCase'

Upvotes: 0

Views: 415

Answers (1)

Eliseo Parodi Almaraz
Eliseo Parodi Almaraz

Reputation: 26

The way to call the tests in python is very similar to the way you import things in that language. For example, if you want to import that TestCase in your file and assuming it is in the tests.py file, you should write import CreateWalletTestCase from api.tests in the elegant way. You can also write import api.tests.CreateWalletTestCase, to import it.

So, to call that specific test, you should write the command in the following way:

python manage.py test api.tests.CreateWalletTestCase.test_create_wallet

You can check the Django Documentation here about running tests.

It is not necessary to delete the __init__.py file.

Upvotes: 1

Related Questions