对于单元测试来说这是非常新的东西,所以这可能很容易,但是我不确定如何在函数中模仿自变量。
我要测试的功能:
class dataFeed:
def generateURL(self, ticker, days, period):
return "https://www.google.com/finance/getprices?i=" + str(period) + "&p=" + str(days) + "d&f=d,o,h,l,c,v&df=cpct&q=" + ticker
Run Code Online (Sandbox Code Playgroud)
测试类别:
import unittest
from dataFeed import dataFeed as df
class TestCases(unittest.TestCase):
def test(self):
self.assertEqual(df.generateURL("AAPL", 2, 5), "https://www.google.com/finance/getprices?i=5&p=2d&f=d,o,h,l,c,v&df=cpct&q=AAPL")
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
我得到的输出是这样的:
ERROR: test (__main__.TestCases)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\ian\Documents\Capstone\Components\testing.py", line 9, in test
self.assertEqual(df.generateURL("AAPL", 2, 5), "https://www.google.com/finance/getprices?i=5&p=2d&f=d,o,h,l,c,v&df=cpct&q=AAPL")
TypeError: unbound method generateURL() must be called with dataFeed instance as first argument (got str instance instead)
Run Code Online (Sandbox Code Playgroud) 我一直在搜索试图找到一个函数的文档,这个函数将从iGraph图中返回一个列表顶点但却找不到.有谁知道如何从iGraph图中获取顶点列表?
所以出于某种原因,我在select语句中使用占位符时遇到错误.
def get_id(table_name, id_name):
db = sqlite3.connect('test_db')
max_id = db.execute('''SELECT max(?) FROM ?''', (id_name, table_name)).fetchone()[0]
if not max_id:
primary_id = 0
else:
primary_id = max_id + 1
Run Code Online (Sandbox Code Playgroud)
此函数返回此错误:
File "test.py", line 77, in get_id
max_id = db.execute('''SELECT max(?) FROM ?''', (id_name, table_name)).fetchone()[0]
sqlite3.OperationalError: near "?": syntax error
Run Code Online (Sandbox Code Playgroud)
另外,以防万一:
id_name = 'pos_id'
table_name = 'Poscar'
Run Code Online (Sandbox Code Playgroud)