[Python Test] Use pytest fixtures to reduce duplicated code across unit tests

In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resource requirements. For example, an instantiated object from a class. You will learn how to create the instance of the class one time as a fixture and reuse that object across all your tests. This results in faster tests, eliminates duplicate code, and uses less resources when running your tests.

"""
Python class for a self-driving car.
Suitable for disrupting automotive industry
"""

class Car(object):

    def __init__(self, speed, state):
        self.speed = speed
        self.state = state

    def start(self):
        self.state = "running"
        return self.state

    def turn_off(self):
        self.state = "off"
        return self.state

    def accelerate(self):
        self.speed += 10
        return self.speed

    def stop(self):
        self.speed = 0
        return self.speed

test:

"""
Tests for Car class
"""

import pytest
from car import Car

class TestCar(object):

    """
    default scope is "function" which means
    foreach test, it will have its own scope
    "module" ref to class itself, so it sharing
    the same instance
    """

    @pytest.fixture(scope="module")
    def my_car(self):
        return Car(0, "off")

    def test_start(self, my_car):
        my_car.start()
        assert my_car.state == "running"

    def test_turn_off(self, my_car):
        my_car.turn_off()
        assert my_car.state == "off"

    def test_accelerate(self, my_car):
        my_car.accelerate()
        assert my_car.speed == 10

    """
    This one will failed because we are using fixture
    scope as "module", my_car.speed == 20
    """
    def test_accelerate1(self, my_car):
        my_car.accelerate()
        assert my_car.speed == 10

    def test_stop(self, my_car):
        my_car.stop()
        assert my_car.speed == 0

原文地址:https://www.cnblogs.com/Answer1215/p/8460290.html

时间: 2024-08-02 02:31:33

[Python Test] Use pytest fixtures to reduce duplicated code across unit tests的相关文章

python 内建函数 filter,map和reduce

python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用.下面分别介绍. 1.filter filter(bool_func,seq):此函数的功能类似过滤器.调用一个布尔函数bool_func来迭代遍历每个seq中的元素:返回一个使bool_seq返回值为true的元素的序列. 例如 : 从[1,2,3,4,5,6,7,8,9]序列中获取被3整除的序列

python中filter、map、reduce的区别

python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内置函数. lambda:实现python中单行最小函数. g = lambda x: x * 2 #相当于 def g(x): return x*2 filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的ite

python学习进度11(map/reduce)

Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念. 我们先看map.map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. 举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个list [1

IntelliJ IDEA “Finds duplicated code”提示如何关闭

发现重复的代码这个提示真的很烦啊,我们怎么关闭他呢. 设置在这里: Settings -> Editor -> Inspections -> General -> Duplicated Code

Duplicated Code

[Duplicated Code]

Python黑帽编程1.2 基于VS Code构建Python开发环境

Python黑帽编程1.2 基于VS Code构建Python开发环境 Python黑帽编程1.2  基于VS Code构建Python开发环境 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and Defense with Python>一书,为了解决很多同学对英文书的恐惧,解决看书之后实战过程中遇到的问题而作.由于原书很多地方过于简略,笔者根据实际测试情况和最新的技术发展对内容做了大量的变更,当然最重要的是个人

【好文要转】Python:模拟登录以获取新浪微博OAuth的code参数值

[转自]http://www.tuicool.com/articles/zAz6zi [原文]http://blog.segmentfault.com/hongfei/1190000000343851 在使用新浪微博提供的API时,首先需要通过认证和授权,关于这部分,大家可以参考下 这篇文章 在完成以上步骤后,大家会发现每次要使用微博API之前,都需要我们手动输入code参数的值才行. 其中,code参数的值是在浏览器的地址栏中,也就是说,只要我们能使用代码正确地模拟浏览器发包,那么也就能得到c

IntelliJ IDEA “duplicated code fragment(6 lines long)”提示如何关闭

Settings —> Editor —> Inspections —> General —> Duplicated Code fragment  把对应的勾去掉 原文地址:https://www.cnblogs.com/zhaobao1830/p/12217368.html

Python单元测试框架之pytest -- fixtures

fixtures不太好翻译,可看作是夹心饼干最外层的两片饼干.通常用setup/teardown来表示.它主要用来包裹测试用例,为什么需要这样的饼干呢?我们以web自动化测试为例,例如,要测试的某系统需要登录/退出.那么每一条用例执行前都需要登录,执行完又都需要退出,这样每条用例重复编写登录和退出就很麻烦,当然,你也可以把登录和退出封装为方法调用,但是每个用例中都写调用也很麻烦.有了fixtures就变得简便很多. 测试函数