前言
小伙伴们好呀,我又来了我们今天聊聊关于pytest前后置应用,使用过unittest的小伙伴们都知道,setup和teardown是用来处理用例的开始前工作和结束后的工作,其中还有setupclass和teardownclass是保证执行所以的用例都只执行1次前置和后置,使用起来非常方便,那么学习pytest强大的测试框框,肯定也有这个功能,并且还比unittest的简单不少。
pytest中的前置
pytest比较强大,提供了不仅仅一种方法的前置和后置:
- setup_module、teardown_module
- setup_function、teardown_function
- setup_class、teardown_class
- setup_method、teardown_method
- setup、teardown
光看上面的内容,我看起来一定很困惑。我不知道什么时候该用。我将通过实例逐一介绍。
[En]
Just looking at the above content, I must look confused. I don’t know when to use it. I’ll introduce it one by one through examples.
setup、teardown
先介绍第一个大家都比较熟悉的与unittest中的书写一致,这个可以在类中使用,也可以在类外进行使用。
该方法每条用例都会执行
import pytest
def setup():
print('这是测试用例的前置')
def teardown():
print('这是测试用例的后置')
def test01():
print('用例01')
def test02():
print('用例02')
if __name__ == '__main__':
pytest.main(['-s'])

setup_module、teardown_module
该方法表示只能类外面执行用例过程中,只执行1次。相当于unittest中的setupclass和teardownclass方法
import pytest
def setup_module():
print('这是测试用例的前置')
def teardown_module():
print('这是测试用例的后置')
def test01():
print('用例01')
def test02():
print('用例02')
if __name__ == '__main__':
pytest.main(['-s','test_02.py'])

setup_function、teardown_function
此方法意味着在类外部执行用例的过程中,每次都会执行前向和后向。
[En]
This method means that the front and post are executed each time during the execution of the use case outside the class.
import pytest
def setup_function():
print('这是测试用例的前置')
def teardown_function():
print('这是测试用例的后置')
def test01():
print('用例01')
def test02():
print('用例02')
if __name__ == '__main__':
pytest.main(['-s','test_02.py'])

setup_method、teardown_method
此方法意味着,在类中执行每个测试用例之前,将执行前测试和后测试。
[En]
This method means that the pre-test and post-test will be executed before each test case is executed in the class.
# coding:utf-8
import pytest
class Test():
def setup_method(self):
print('这是setup函数前置内容')
def teardown_method(self):
print('这是teardown函数后置内容')
def test01(self):
print('这是测试用例1')
def test02(self):
print('这是测试用例2')
if __name__ == '__main__':
pytest.main(['-s','test_01.py'])

setup_class、teardown_class
此方法意味着在类中执行测试用例之前,只执行一次测试前端和测试后端。
[En]
This method means that the test front and test post are executed only once before the test case is executed in the class.
# coding:utf-8
import pytest
class Test():
def setup_class(self):
print('这是setup函数前置内容')
def teardown_class(self):
print('这是teardown函数后置内容')
def test01(self):
print('这是测试用例1')
def test02(self):
print('这是测试用例2')
if __name__ == '__main__':
pytest.main(['-s','test_01.py'])

组合混用
上面描述了每种方法的单独用法,那么如果所有这些方法一起使用会怎么样呢?会发生什么事?
[En]
The separate usage of each method is described above, so what if all of these methods are used together? What’s gonna happen?
setup_class和setup_method、setup混合
# coding:utf-8
import pytest
class Test():
def setup_method(self):
print('这是setup_method用例前置内容')
def setup_class(self):
print('这是setup_class用例前置内容')
def setup(self):
print('这是setup用例前置内容')
def teardown_class(self):
print('这是teardown_class用例后置内容')
def teardown_method(self):
print('这是teardown_method用例后置内容')
def teardown(self):
print('这是teardown用例后置内容')
def test01(self):
print('这是测试用例1')
def test02(self):
print('这是测试用例2')
if __name__ == '__main__':
pytest.main(['-s','test_01.py'])

通过上面测试结果可以看出来,其中执行顺序:setup_class>>setup_method>>setup,其中setup和setup_method都是表示执行每条用例前都需要进行执行前置内容
setup_module、setup_function组合
import pytest
def setup():
print('这是setup测试用例前置内容')
def setup_function():
print('这是setup_function测试用例前置内容')
def setup_module():
print('这是setup_module测试用例前置内容')
def teardown_module():
print('这是teardown_module测试用例后置内容')
def teardown():
print('这是teardown测试用例后置内容')
def teardown_function():
print('这是teardown_function测试用例后置内容')
def test01():
print('用例01')
def test02():
print('用例02')
if __name__ == '__main__':
pytest.main(['-s','test_02.py'])

总结:
1、setup_class和setup_module执行用例时,只执行一次前置和后置
2、setup_class,setup_method,setup是在类中执行的
3、setup_module,setup_function,setup是在类外执行的
4、其中setup类中,类外都可以执行。
阿六通过简单的例子介绍了pytest的setup和teardown的使用方法和类型,希望大家能够自己行动起来哟,毕竟实践是真理。
如果你喜欢这篇文章,你可以用小手致富给七叔点赞和收藏,顺便加一句关注,注意七叔不要迷路。
[En]
If you like this article, you can use the little hand to get rich to give uncle seven a like and collection, by the way, add a concern, pay attention to uncle seven not to get lost.

Original: https://blog.csdn.net/qishuzdh/article/details/124806957
Author: 自动化测试七叔
Title: pytest—setup和teardown简单用法
相关阅读
Title: pytorch几种乘法的区别
pytorch几种乘法的区别
torch.mul()是矩阵的点乘,即对应的位相乘,要求shape一样, 返回的还是个矩阵
torch.mm()是矩阵正常的矩阵相乘,(a, b)* ( b, c ) = ( a, c )
torch.dot()类似于mul(),它是向量(即只能是一维的张量)的对应位相乘再求和,返回一个tensor数值
torch.mv()是矩阵和向量相乘,类似于torch.mm()
np.dot(x,y)
如果x,y都是一维张量,那么np.dot(x,y)是∑ i = 0 m x i y i \sum_{i=0}^{m}x_i y_i ∑i =0 m x i y i
矩阵乘和向量乘
矩阵乘法符号说明matrix product·一般矩阵乘法哈达玛积⊙或○逐元素对应相乘克罗内克积$ \otimes $A的每个元素逐个与B矩阵相乘 向量乘法符号说明内积 点积·逐元素对应相乘之积的和 结果为标量外积$ \otimes $结果为矩阵叉乘Cross Product×结果为向量
点积
点积是两个矩阵相同位置的 按元素乘积的 和
点乘的几何意义:点乘可以用来计算两个向量之间的角度。[En]
The geometric meaning of point multiplication: point multiplication can be used to calculate the angle between two vectors.
x = tf.constant([0., 1., 2., 3.])
y= tf.ones(4)
tf.tensordot(x, y, axes=1)
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
tf.reduce_sum(x * y)
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
矩阵-向量积
一个矩阵乘一个列向量 相当于 矩阵的列向量的线性组合
一个行向量 乘以矩阵 相当于矩阵的行向量的线性组合
B.shape, C.shape, tf.linalg.matvec(C,B)
(TensorShape([3, 4]),
TensorShape([4]),
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([23., 23., 17.])>)
矩阵-矩阵乘法
一般矩阵乘法
A,B,tf.matmul(A,B)
<tf.Tensor: shape=(3, 4), dtype=float64, numpy=
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])>
<tf.Tensor: shape=(4, 3), dtype=float64, numpy=
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])>
<tf.Tensor: shape=(3, 3), dtype=float64, numpy=
array([[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.]])>
Original: https://blog.csdn.net/qq_40887371/article/details/123966695
Author: simpsun
Title: pytorch几种乘法的区别
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/334110/
转载文章受原作者版权保护。转载请注明原作者出处!