1 在settings.json中添加如下
"python.testing.unittestArgs": [
"-v",
"-s",
"./",
"-p",
"*_test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
-s后的参数代表启动路径
-p 代表测试文件pattern
2 创建测试文件
as_test.py文章来源:https://www.toymoban.com/news/detail-806965.html
注意函数名要以'test_'开头文章来源地址https://www.toymoban.com/news/detail-806965.html
import unittest
class TestStringMethods(unittest.TestCase):
def test_add(self):
a = 1
b = 2
self.assertEqual(a,1)
def test_upper(self):
# self.assertEqual('foo'.upper(), 'FOO')
print("here")
print("dasdasdwadawdwad")
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
到了这里,关于vscode上对python进行单元测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!