一个实例
# content of test_sample.py
def func(x):
return x+1
def test_func():
assert func(3) == 5
再一个实例
当需要编写多个测试样例的时候,我们可以将其放到一个测试类当中,如:
# content of test_class.py
class TestClass:
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
我们可以通过执行测试文件的方法,执行上面的测试:
$ py.test -q test_class.py
.F [100%]
======================================================================== FAILURES =========================================================================
___________________________________________________________________ TestClass.test_two ____________________________________________________________________
self = <testAA.TestClass instance at 0x10dd68d40>
def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
testAA.py:8: AssertionError
1 failed, 1 passed in 0.05 seconds
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper