stock.py 这个文件,我只写了注释(的开头),大部分注释内容和函数都是copilot
# split a string and extract the environment variable from it
# input can be '', 'path=abc', 'path=abc;path=def', 'path=abc;path=def;path=ghi'
# output is a list of tuple environment variables(key and value)
def split_env_var(env_var):
env_var_list = []
if env_var == '':
return env_var_list
else:
env_var_list = env_var.strip(';').split(';')
env_var_list = [tuple(x.split('=')) for x in env_var_list]
return env_var_list
再新建一个 stock_test.py,写一行注释,Ctrl+L(或者调用 Open compiletion Panel)产生了很多的单元测试样例。
#this is test of stock.py
import unittest
from stock import split_env_var
class TestStock(unittest.TestCase):
def test_empty(self):
self.assertEqual(split_env_var(''), [])
def test_empty2(self):
self.assertEqual(split_env_var(';'), [('',)])
def test_one(self):
self.assertEqual(split_env_var('path=abc;'), [('path', 'abc')])
def test_two(self):
self.assertEqual(split_env_var('path=abc;path=def'), [('path', 'abc'), ('path', 'def')])
def test_three(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi')])
def test_four(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi;'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi')])
def test_five(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi;path=jkl'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi'), ('path', 'jkl')])
def test_six(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi;path=jkl;'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi'), ('path', 'jkl')])
def test_seven(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi;path=jkl;path=mno'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi'), ('path', 'jkl'), ('path', 'mno')])
def test_eight(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi;path=jkl;path=mno;'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi'), ('path', 'jkl'), ('path', 'mno')])
def test_nine(self):
self.assertEqual(split_env_var('path=abc;path=def;path=ghi;path=jkl;path=mno;path=pqr'), [('path', 'abc'), ('path', 'def'), ('path', 'ghi'), ('path', 'jkl'), ('path', 'mno'), ('path', 'pqr')])
if __name__ == '__main__':
unittest.main()
再装一个 Python Test Explorer
插件用来进行 GUI test
附上我的 python test gui 配置
.vscode/settings.jon
{
"python.testing.unittestEnabled": true,
"python.testing.unittestArgs": [
"-v",
"-s",
"./tmp",
"-p",
"*_test.py"
],
}
有了 copilot 这个代码神器,之后的代码效率可以提升很多,之后要学会和这个超级智能助手交流,使用 pilot chat 功能,阅读开源代码的时候如同有一个老师在身旁, 更加有底气。