selenium第六章unittest

selenium第六章
calculator.py

class Calculator:
    def __init__(self,a,b):
        self.a=int(a)
        self.b=int(b)
    def add(self):
        return self.a+self.b
    def sub(self):
        return self.b-self.a
    def mul(self):
        return self.a*self.b
    def div(self):
        return self.a/self.b

test_calculator.py

from calculator import Calculator
def test_add():
    c=Calculator(3,5)
    result=c.add()
    assert result==7,'加法运算失败'

def test_sub():
    c=Calculator(5,3)
    result=c.add()
    assert result==1,'减法运算失败'

if __name__=='__main__':
    test_add()

unittest

import unittest
from calculator import Calculator
class TestCalculator(unittest.TestCase):
    def test_add(self):
        c=Calculator(3,5)
        result=c.add()
        self.assertEqual(result,8)

    def test_sub(self):
        c=Calculator(5,4)
        result=c.sub()
        self.assertEqual(result,1)
    def test_mul(self):
        c=Calculator(5,2)
        result=c.mul()
        self.assertEqual(result,10)
    def test_div(self):
        c=Calculator(6,3)
        result=c.div()
        self.assertEqual(result,2)

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

在这里插入图片描述

import unittest
from calculator import Calculator
class TestCalculator(unittest.TestCase):
    def setUp(self):
        print("start")
    def tearDown(self):
        print("end")
    def test_add(self):
        c=Calculator(3,5)
        result=c.add()
        self.assertEqual(result,8)

    def test_sub(self):
        c=Calculator(5,4)
        result=c.sub()
        self.assertEqual(result,1)
    def test_mul(self):
        c=Calculator(5,2)
        result=c.mul()
        self.assertEqual(result,10)
    def test_div(self):
        c=Calculator(6,3)
        result=c.div()
        self.assertEqual(result,2)

if __name__=='__main__':
    suit=unittest.TestSuite()
    suit.addTest(TestCalculator("test_add"))
    suit.addTest(TestCalculator("test_sub"))
    suit.addTest(TestCalculator("test_mul"))
    suit.addTest(TestCalculator("test_div"))

    runner=unittest.TextTestRunner()
    runner.run(suit)

discover方法找到测试模块

import unittest
test_dir='./test_case'
suits=unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
if __name__=='__init':
    runner=unittest.TextTestRunner()
    runner.run(suits)

跳过测试和预期失败
‘@’ 用做函数的修饰符,可以在模块或者类的定义层内对函数进行修饰
同样适用于测试类

import unittest
class Mytest(unittest.TestCase):
    @unittest.skip("直接跳过测试")
    def test_skip(self):
        print("test_aaa")
    @unittest.skipIf(3>2,"条件为真跳过")
    def test_skip_if(self):
        print("test_bbb")
    @unittest.skipUnless(3>2,"条件为真时执行测试")
    def test_skip_unless(self):
        print("test_ccc")
    @unittest.expectedFailure
    def test_expect_failure(self):
        self.assertEqual(2,3)
if __name__=='__main__':
    unittest.main()

编写web自动化测试

import unittest
from time import sleep
from selenium import webdriver
class TestBaidu(unittest.TestCase):
    def setUp(self) -> None:
        self.driver=webdriver.Chrome()
        self.base_url="https://www.baidu.com"
    def test_search_key_selenium(self):
        self.driver.get(self.base_url)
        self.driver.find_element_by_id("kw").send_keys("selenium")
        self.driver.find_element_by_id("su").click()
        sleep(2)
        title=self.driver.title
        self.assertEqual(title,"selenium_百度搜索")
    def test_search_key_unnitest(self):
        self.driver.get(self.base_url)
        self.driver.find_element_by_id("kw").send_keys("unittest")
        self.driver.find_element_by_id("su").click()
        sleep(2)
        title = self.driver.title
        self.assertEqual(title, "unittest_百度搜索")
    def tearDown(self) -> None:
        self.driver.quit()
if __name__=='__main__':
    unittest.main()

模块化和前置动作优化

import unittest
from time import sleep
from selenium import webdriver
class TestBaidu(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver=webdriver.Chrome()
        cls.base_url="https://www.baidu.com"
    def baidu_search(self,search_key):
        self.driver.get(self.base_url)
        self.driver.find_element_by_id("kw").send_keys(search_key)
        self.driver.find_element_by_id("su").click()
        sleep(2)
    def test_search_key_selenium(self):
        search_key="selenium"
        self.baidu_search(search_key)
        self.assertEqual(self.driver.title,search_key+"百度搜索")
    def test_search_key_unnitest(self):
        search_key = "unittest"
        self.baidu_search(search_key)
        self.assertEqual(self.driver.title, search_key + "百度搜索")
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
if __name__=='__main__':
    unittest.main()

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页