【pytest】单元测试文件的写法

前言

可怜的宾馆,可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》

在这里插入图片描述


         \;\\\;\\\;

test_1或s_test格式

要么 test_前缀 在前,要么 _test后缀 在后!

#test_1.py
def test_1():
    name='aa'
    assert 'bb'==name

def test_2():
    name='a'
    assert 'bcb'==name

def test_3():
	a = 1
	assert a == 2


def test_4():
	a = 4
	assert a == 2
	assert 'a' in 'abc'
	assert 'a' not in 'abc'
	assert 'a' is not True
	assert 'a' is False

右击可以单独运行某个函数,看看哪个错了!

class TestTint:
	def test_5(self):
		a = 1
		assert a == 2

	def test_6(self):
		a = 1
		assert a == 2

	def test_7(self):
		a = 1
		assert a == 2

在这里插入图片描述

         \;\\\;\\\;

非测试文件

如果是按pytest格式的文件名,但是内容不是测试的话,那么会出现(没有发现测试)

#test_calc.py
a = 1
b = 2
print(a + b)

在这里插入图片描述
         \;\\\;\\\;

@pytest.fixture()装饰器

import pytest


@pytest.fixture(scope='function')
def fixture1():
	print('前置步骤1')
	return 4


@pytest.fixture(scope='function')
def fixture2():
	print('前置步骤2')
	return 2

@pytest.fixture(scope='function',autouse=True)
def fixture3():
	print('前置步骤3')
	return 2



def test_1(fixture1,fixture2):
	assert fixture1 == 2
	assert fixture2 == 2


def test_2(fixture3):
	assert fixture3 == 2


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

可以在测试函数的位置,右击运行test_1或test_2函数

         \;\\\;\\\;

pytest+selenium

关于selenium使用的edge驱动器,版本要和电脑上装的edge版本一致!

pytest类要以Test为前缀

#test_f.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import pytest


# 前置方法
@pytest.fixture(scope='class')
def driver():
	driver = webdriver.Edge(executable_path=r"C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Scripts\msedgedriver.exe")
	#C:\ProgramData\anaconda3\Scripts\msedgedriver.exe
	return driver


class TestSpider:

	# fixture函数作为形参
	def test_baidu(self, driver):
		driver.get('https://www.baidu.com/')
		title = driver.title
		url = driver.current_url

		text = driver.find_element(By.CSS_SELECTOR, "a[href*='news.baidu.com']").text

		button = driver.find_element(By.ID, 'su').get_attribute('value')

		# 检测
		assert title == '百度一下,你就知道'
		assert url == 'https://www.baidu.com/'
		assert text == '新闻'
		assert button == '百度一下'

# sleep(3)
# driver.close()


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

运行命令 pytest test_f.py

相关推荐

  1. 测试中调用别人服务,单元测试写法

    2023-12-12 16:52:03       60 阅读
  2. pytest unittest temp path单元测试创建临时文件

    2023-12-12 16:52:03       40 阅读
  3. Python单元测试框架:unittest与pytest深度对比

    2023-12-12 16:52:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-12 16:52:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-12 16:52:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-12 16:52:03       82 阅读
  4. Python语言-面向对象

    2023-12-12 16:52:03       91 阅读

热门阅读

  1. linux通信

    2023-12-12 16:52:03       50 阅读
  2. Nginx的stream配置

    2023-12-12 16:52:03       61 阅读
  3. 在大量数据中查找重复的两个数

    2023-12-12 16:52:03       65 阅读
  4. python——第十六天

    2023-12-12 16:52:03       52 阅读
  5. 关于牛顿法计算潮流问题bug解决

    2023-12-12 16:52:03       66 阅读
  6. ffmpeg相关命令

    2023-12-12 16:52:03       61 阅读