在django webapp中手动测试期间创建其他表

hcoder 测试交流1 16字数 3670阅读12分14秒阅读模式
摘要

我正在用selenium在django web应用程序中创建一些测试。
Django创建一个包含所有表的测试数据库。
然而,就我而言,我有一个模型用户。
我希望django在我。。。

我正在用selenium在django web应用程序中创建一些测试。
Django创建一个包含所有表的测试数据库。文章源自玩技e族-https://www.playezu.com/190365.html

然而,就我而言,我有一个模型用户。
我希望django在终端中运行此命令时也创建users表
python3.9管理。py测试-v3文章源自玩技e族-https://www.playezu.com/190365.html

此命令出错:文章源自玩技e族-https://www.playezu.com/190365.html

Running post-migrate handlers for application contenttypes
Adding content type 'contenttypes | contenttype'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application sessions
Adding content type 'sessions | session'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application pygiustizia
Adding content type 'pygiustizia | member'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
System check identified no issues (0 silenced).
test_login (pygiustizia.tests.test_views_topics.ViewsTopicsTestCase) ... nel costruttore model Users
ERROR
======================================================================
ERROR: test_login (pygiustizia.tests.test_views_topics.ViewsTopicsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/nicola/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nicola/.local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/cursors.py", line 148, in execute
    result = self._query(query)
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/cursors.py", line 310, in _query
    conn.query(q)
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/connections.py", line 775, in _read_query_result
    result.read()
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "/home/nicola/.local/lib/python3.9/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1146, "Table 'test_civiledb.users' doesn't exist")

编辑
我可以访问我想要手动创建的表用户,以便通过该模型进行测试。文章源自玩技e族-https://www.playezu.com/190365.html

from django.db import models
from django.db import connection
import pymysql
class Users(models.Model):
    firstname=models.CharField(max_length=100)
    lastname=models.CharField(max_length=100) 
    username=models.CharField(max_length=50)
    password=models.CharField(max_length=255)
    admin=models.IntegerField()
    createdAt=models.DateTimeField(db_column="created_at")
    def __init__(self,*args, **kwargs):
        super().__init__(*args, **kwargs)
        print("nel costruttore model Users")
    def getAdmin(self):
        return self.admin
    class Meta:
        db_table = 'users'
    def getUser(self,username):
        cursor = connection.cursor()
        cursor.execute('''SELECT id, 
                            username, 
                            password,
                            admin,
                            firstname,
                            lastname,
                            created_at 
                            FROM users 
                            WHERE username = %s''',[username])
        result = cursor.fetchone()
        return result
    def setPassword(self,userId,password):
        cursor = connection.cursor()
        try:
            cursor.execute('''UPDATE users  
                            SET password = %s 
                            WHERE id = %s''',[password,userId])
            connection.commit()
        except Exception as e:
            print(e)
            connection.rollback()
            return False
        finally:
            connection.close()
        return True

解决方案是创建;sql创建;模式在安装程序中的testcase中运行它,并在拆卸时删除它。文章源自玩技e族-https://www.playezu.com/190365.html

软件测试的功能测试文章源自玩技e族-https://www.playezu.com/190365.html

注意:本文法律责任由该作者承担,侵权请联系2523030730▷诈骗举报◁▷新闻不符◁▷我要投稿◁
  • 我们QQ群
  • QQ扫一扫
  • weinxin
  • 微信公众号
  • 公众号扫一扫
  • weinxin
评论  1  访客  1
    • Sören Weber
      Sören Weber 9 未知系统 IANA

      我认为您在这里寻找的概念是非托管模型。通过设置适当的元选项,您可以告诉Django它不应该为您处理表生命周期。
      要回答您的问题,请使用 方案编辑器 在测试设置和拆卸似乎是一个特别干净的方式来实现你所寻找的。

    匿名

    发表评论

    匿名网友

    确定