Python—比较两个函数的输出的单元测试
我试图执行一个单元测试来检查两个查询的计数是否相同 下面是我的代码,我已经到位。有谁能帮助我用python执行单元测试,检查这两个函数中的计数是否相同 base.pyPython—比较两个函数的输出的单元测试,python,python-3.x,unit-testing,Python,Python 3.x,Unit Testing,我试图执行一个单元测试来检查两个查询的计数是否相同 下面是我的代码,我已经到位。有谁能帮助我用python执行单元测试,检查这两个函数中的计数是否相同 base.py import psycopg2 def q1(): dwh_connection = psycopg2.connect(connection_details) cur = dwh_connection.cursor() cur.execute("select count(*) from table_1 limit 4
import psycopg2
def q1():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
cur.execute("select count(*) from table_1 limit 4")
dwh_connection.close()
def q2():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
cur.execute("select count(*) from table_2 limit 4")
dwh_connection.close()
谢谢
编辑:
错误消息:
下面是我的单元测试代码
import unittest
import base import q1, q2
class TestStringMethods(unittest.TestCase):
def test_queries(self):
self.assertEqual(q1(),q2())
if __name__ == '__main__':
unittest.main()
base.py的代码
import psycopg2
def q1():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
cur.execute("select count(*) from table_1 limit 4")
dwh_connection.close()
def q2():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
cur.execute("select count(*) from table_2 limit 4")
dwh_connection.close()
您可以使用以下内容进行单元测试
import unittest
class TestExample(unittest.TestCase):
def test_queries(self):
self.assertEqual(q1(), q2())
if __name__ == '__main__':
unittest.main()
def q1():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
result = cur.execute("""select count(*) from table_1 limit 4""").fetchone()
dwh_connection.close()
return result
但是,您的函数现在不会返回任何内容。把它们改成
import unittest
class TestExample(unittest.TestCase):
def test_queries(self):
self.assertEqual(q1(), q2())
if __name__ == '__main__':
unittest.main()
def q1():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
result = cur.execute("""select count(*) from table_1 limit 4""").fetchone()
dwh_connection.close()
return result
您可以使用以下内容进行单元测试
import unittest
class TestExample(unittest.TestCase):
def test_queries(self):
self.assertEqual(q1(), q2())
if __name__ == '__main__':
unittest.main()
def q1():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
result = cur.execute("""select count(*) from table_1 limit 4""").fetchone()
dwh_connection.close()
return result
但是,您的函数现在不会返回任何内容。把它们改成
import unittest
class TestExample(unittest.TestCase):
def test_queries(self):
self.assertEqual(q1(), q2())
if __name__ == '__main__':
unittest.main()
def q1():
dwh_connection = psycopg2.connect(connection_details)
cur = dwh_connection.cursor()
result = cur.execute("""select count(*) from table_1 limit 4""").fetchone()
dwh_connection.close()
return result
编写单元测试时,通常不鼓励测试数据库。这是因为您基本上是在测试数据库,在运行之间可能会出现其他状态问题。单元测试在运行之间应该是无状态的,这基本上将它们与状态联系起来。我建议在这里查看
mock
:编写单元测试时通常不鼓励测试数据库。这是因为您基本上是在测试数据库,在运行之间可能会出现其他状态问题。单元测试在运行之间应该是无状态的,这基本上将它们与状态联系起来。我建议您在这里查看mock
:谢谢您的回复。我得到一个错误,表示q1未定义(参考行“self.assertEqual(q1(),q2())”)。你能在这方面帮忙吗。谢谢..@darkhorse:q1
是第二个代码示例中的函数。您是否已将其添加到代码中?是的,我的初始代码(base.py)中有两个函数q1和q2。单独执行时,运行良好。当我在UnitCase中调用这些函数时,我得到一个错误,如下文件“/Users/user/unit/test\u calc.py”,test\u查询self.assertEqual(q1(),q2())NameError中的第10行:名称“q1”不是defined@darkhorse:如果它们位于另一个文件中,则需要导入。抱歉,没有使用导入功能。我已经在单元测试代码中导入了这个文件,现在已经更新了。谢谢你的回复。我得到一个错误,表示q1未定义(参考行“self.assertEqual(q1(),q2())”)。你能在这方面帮忙吗。谢谢..@darkhorse:q1
是第二个代码示例中的函数。您是否已将其添加到代码中?是的,我的初始代码(base.py)中有两个函数q1和q2。单独执行时,运行良好。当我在UnitCase中调用这些函数时,我得到一个错误,如下文件“/Users/user/unit/test\u calc.py”,test\u查询self.assertEqual(q1(),q2())NameError中的第10行:名称“q1”不是defined@darkhorse:如果它们位于另一个文件中,则需要导入。抱歉,没有使用导入功能。我已经在单元测试代码中导入了该文件,现在已更新。。