1、新建单元测试文件

右键->"new"->"Python File",如下图:

PyCharm 单元测试

 

 

2、代码如下:

import unittest
from flask import current_app
from apps.unit_test import app


class MyTestCase(unittest.TestCase):
    # 该方法会首先执行,方法名固定
    def setUp(self):
        self.app = app
        self.app_context = self.app.app_context()
        self.app_context.push()

    # 该方法会在测试代码执行完后执行,方法名固定
    def tearDown(self):
        self.app_context.pop()

    # 测试应用实例是否存在
    def test_app_exist(self):
        self.assertFalse(current_app is None)


if __name__ == '__main__':
    unittest.main()

 

3、运行配置

界面右上角->"Edit Configurations...",如下图:

PyCharm 单元测试

 

 

 

然后,如下图:

 PyCharm 单元测试

 

 

 PyCharm 单元测试

 

 

 点击确认即可

 

4、运行测试

PyCharm 单元测试

 

 

 完成

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-01-20
猜你喜欢
  • 2022-01-20
  • 2022-12-23
  • 2021-06-27
  • 2022-12-23
  • 2021-12-21
  • 2021-12-04
  • 2022-03-07
相关资源
相似解决方案