In this lesson you will create a new project with a virtual environment and write your first unit test with pytest. In doing so, you will learn:

  • install pytest
  • organize your project to support automated test discovery
  • setup Visual Code to use pytest as your test engine
  • best practice naming conventions for tests in Python

Install:

sudo apt install virtualenv

Create virtualenv inside project folder:

virtualenv -p /usr/local/bin/python3 .env

Source to the env:

source .env/bin/activate

Install the lib:

pip install pytest pylint

VSCode workspace settings:

{
"python.pythonPath": "${workspaceFolder}/.env/bin/python",
"python.unitTest.pyTestEnabled": true,
"python.unitTest.pyTestArgs": [
"--ignore=.env",
"-s"
],
"python.envFile": "${workspaceFolder}/.envFile"
}

Code to test:

import pytest
import common_math class TestCommonMath(object): def test_add(self):
result = common_math.add(1,2)
assert result == 3

Testing code:

def add(num1, num2):
return num1 + num2

[Python + Unit Testing] Write Your First Python Unit Test with pytest的更多相关文章

  1. 10 Unit Testing and Automation Tools and Libraries Java Programmers Should Learn

    转自:https://javarevisited.blogspot.com/2018/01/10-unit-testing-and-integration-tools-for-java-program ...

  2. Java Unit Testing - JUnit & TestNG

    转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...

  3. Live Unit Testing

    Live Unit Testing 相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是 ...

  4. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  5. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  6. [Java Basics3] XML, Unit testing

    What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...

  7. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

  8. [Unit Testing] AngularJS Unit Testing - Karma

    Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...

  9. Unit testing Cmockery 简单使用

    /********************************************************************** * Unit testing Cmockery 简单使用 ...

随机推荐

  1. (十一)Unity5新特性----实战2D游戏

    孙广东  2015.7.11 在本教程中,将了解到U5新功能.你通过本教程.您将了解下面内容: Changes in Component Access Physics Effectors Adding ...

  2. jsp不通过form和Ajax提交

    在页面里面我们一般都通过form表单和Ajax向后台提交请求,但是我如今页面没有form表单,也不想通过ajax异步提交. 解决方式例如以下:location.href="${rootPat ...

  3. php之将用户信息写入数据库

    session高级应用将用户信息写入到数据库中 首先建立数据库表 在实验数据库sqldb中建立session表,用于存储数据 在根文件夹下建立须要用到的文件(重点是session,class.php这 ...

  4. 英语发音规则---G字母

    英语发音规则---G字母 一.总结 一句话总结: 1.G发[g]音? bag [bæg] n. 袋:猎获物 go [gəʊ] vi. 走:达到 garden ['gɑːd(ə)n] n. 花园 gla ...

  5. SYN-Flood防御方法之一Synproxy

    SYN-Flood攻击: 攻击者发送大量的SYN给服务器. 服务器必须针对每一个SYN请求回送一个SYN-ACK 应答包,此时服务器就必须保持一条半开放的连接,直到接收到一个对应的ACK应答包为止. ...

  6. 踩坑 Windows 解决pip install出现“由于目标计算机积极拒绝,无法连接”的问题

    解决pip install出现“由于目标计算机积极拒绝,无法连接”的问题   可能是使用某软件自动设置了代理, 所以需要手动的取消代理才可以.   在Intel选项中把所有的代理都给去掉就可以了... ...

  7. Noip前紧急抢救

    管他会不会,知道结论就好了 紧急抢救知识 斯特林数 第一类斯特林数 递推公式 \[ S[n][k]=(n-1)\times S[n-1][k]+S[n-1][k-1] \] 处理的问题是将n个数划分为 ...

  8. C语言宏定义#define用法

    #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率,但学生在学习时往往不能 理解该命令的本质,总是在此处产生一些困惑,在编程时误用 ...

  9. 路飞学城Python-Day32

    36-进程池线程池 开多线程实现并发的效率是高的,当用户没有那么多的时候,服务器是可以承受压力的 但是一定要以某种方式来设置并发数,让服务器能够实现稳定的运行,控制服务器的线程数 设置池,往里面放池的 ...

  10. jQuery中的DatePicker今天按钮不起作用

    转载:http://codego.net/63433/ jquery-ui 日期选择器datepicker我想用 jQueryUI 的 DatePicker ,并显示“今天”按钮, 但它不工作,它也不 ...