unittest框架和执行方式

1.setUp、tearDown、self.assertIn和main方法执行

#执行顺序以asc码顺序

#依次执行当前文件对应测试类的所有测试方法

#每个测试方法都会执行一遍setUp和tearDown

class testregister(unittest.TestCase):
    #使用setUP初始化
    def setUp(self):
        self.url = 'http://localhost:8080/jwshoplogin/user/register.do'
        self.userinfo = {'username': 'lilili1',
                         'password': '123456',
                         'email': '123@51testing.com',
                         'phone': '111111111',
                         'question': '最喜欢的书',
                         'answer': '幸福人生'}

    def test_register(self):
        response=requests.post(self.url,data=self.userinfo).json()
        self.assertIn('用户名已经存在',str(response))
    def test_case3_register(self):
        print('注册')
    def test_case2_login(self):
        print('登录')

    def tearDown(self):
        print('还原...')

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

2.测试套执行

#按照加载顺序进行执行

#运行模式改成以当前python文件而不是unittest框架模式运行

class test_mulinterface(unittest.TestCase):
    def setUp(self):
        print('setup')

    def test_case3_register(self):
        print('注册')
    def test_case2_login(self):
        print('登录')

    def tearDown(self):
        print('teardown')

if __name__ == '__main__':
    #声明测试套对象
    suite=unittest.TestSuite()
    #suite.addTest(test_mulinterface('test_case2_login'))
    suite.addTest(test_mulinterface('test_case3_register'))
    #声明运行对象
    runner=unittest.TextTestRunner()
    runner.run(suite)

3.测试文件执行

#执行任意指定路径下的任意相关命名的python测试脚本

#执行每一个文件中所有的测试方法,顺序也是按照asc码顺序进行

if __name__ == '__main__':
    #声明文件所在路径
    testdir='./'
    discover=unittest.defaultTestLoader.discover(testdir,pattern='test3.py')
    #声明运行对象
    runner=unittest.TextTestRunner()
    runner.run(discover)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值