我在运行样本测试时遇到问题。
import unittest
from automation.models import Accountproperty
class TestEx(unittest.TestCase):
def test_get_data_from_database(self):
Accountproperty.objects.count()
if __name__ == "__main__":
unittest.main()
当我从shell运行此测试时,我没有问题:
C:\Users\apodar\PycharmProjects\autoTest>python manage.py shell
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from automation.models import *
>>> Accountproperty.objects.count()
490
>>>
但是当我以这种方式运行测试时,我遇到以下问题:
C:\Users\apodar\PycharmProjects\autoTest>python manage.py test
interface.tests.testEx.TestEx
Creating test database for alias 'default'...
Creating test user...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_get_data_from_database (interface.tests.testEx.TestEx)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\apodar\PycharmProjects\autoTest\interface\tests\testEx.py",
line 7, in test_get_data_from_database
Accountproperty.objects.count()
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line
85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 364,
in count
return self.query.get_count(using=self.db)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line
499, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line
480, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line
894, in execute_sql
raise original_exception
DatabaseError: ORA-00942: table or view does not exist
----------------------------------------------------------------------
Ran 1 test in 0.119s
FAILED (errors=1)
Destroying test database for alias 'default'...
Destroying test user...
Destroying test database tables...
C:\Users\apodar\PycharmProjects\autoTest>
请找到manage.py:
#!/usr/bin/env python
import os
import sys
import django
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "autoTest.settings")
django.setup()
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
settings.py:
"""
Django settings for autoTest project.
Generated by 'django-admin startproject' using Django 1.11.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.append(BASE_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '&v7fm&l%y!7*&=7#!@-as03xy0ux*%7m8$=s&hj)m+6dgbqli('
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'automation',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'autoTest.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'debug': DEBUG,
},
},
]
WSGI_APPLICATION = 'autoTest.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'paymo33',
'USER': 'PAYMOBILE34',
'PASSWORD': 'PAYMOBILE34',
'HOST': '10.33.22.48',
'PORT': '1521',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
有条件的信息: 我使用PyCharm作为IDE,Django 1.11.4,cx-Oracle 5.2,Python 2.7.5,selenium 2.42.0
我的范围是使用selenium创建python测试,它也使用数据库连接(以某种方式在单元测试中引导应用程序)
如果有必要,请告诉我更多信息。请帮忙。非常感谢你。
答案 0 :(得分:0)
在执行SQL语句之前,请不要忘记使用ALTER SESSION SET CURRENT_SCHEMA