[Python + Unit Testing] Write Your First Python Unit Test with pytest
2024-08-24 15:21:46
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
最新文章
- CSS 和 JS 动画哪个更快
- Python 3 and MySQL
- 运维mysql基础
- PHP热身
- jquery 温故而知新 Ul 相关的操作
- jdownload的使用
- ios pyudaren
- 【转】Mac 上 java 究竟在哪里,本文彻底让你搞清楚!
- java连接数据库的解决方法大全(mysql)
- iOS题
- Access数据库导入到mysql数据库中
- C语言中预定义符 __FILE__, __LINE__, __FUNCTION__, __DATE__, __TIME__ 的使用演示
- Android广播——短信拦截
- 懵懂oracle之存储过程2
- Python爬虫入门:URLError异常处理
- 浅析python中socketserver模块使用
- python基础之函数式编程
- ASP.NET Core入门
- python全栈开发day78、79 --bss项目
- Extjs4.2 rest 与webapi数据交互----顺便请教了程序员的路该怎么走