博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Python + Unit Testing] Write Your First Python Unit Test with pytest
阅读量:6976 次
发布时间:2019-06-27

本文共 1009 字,大约阅读时间需要 3 分钟。

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 pytestimport common_mathclass TestCommonMath(object):    def test_add(self):        result = common_math.add(1,2)        assert result == 3

 

Testing code:

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

 

转载地址:http://dnupl.baihongyu.com/

你可能感兴趣的文章
程序员的量化交易之路(25)--Cointrader之MarketData市场数据实体(12)
查看>>
使用 CAS 在 Tomcat 中实现单点登录
查看>>
Podfile 常见语法
查看>>
【原】YUI压缩与CSS media queries下的bug
查看>>
[AWK]使用AWK进行分割字符串以及截取字符串
查看>>
SiteMesh介绍
查看>>
form实现登陆操作
查看>>
SpriteBuilder中如何平均拉伸精灵帧动画的距离
查看>>
poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
查看>>
dojo从asp.net中获取json数据
查看>>
Android:problem opening wizard the selected wizard could not be started
查看>>
PostgreSQL md5 auth method introduce, with random salt protect
查看>>
【spring框架】spring整合hibernate初步
查看>>
JVM调优总结
查看>>
PostgreSQL 9.3 beta2 stream replication primary standby switchover bug?
查看>>
创业思维 - Qunar的故事
查看>>
STM32中GPIO的8种工作模式
查看>>
一分钟了解阿里云产品:先知计划
查看>>
Centos 7环境下源码安装PostgreSQL数据库
查看>>
推荐一款 Flutter Push 推送功能插件
查看>>