vscode上对python进行单元测试
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
注意函数名要以test_开头
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()