- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 36694 个
- 通用积分
- 298.5898
- 学术水平
- 25 点
- 热心指数
- 48 点
- 信用等级
- 22 点
- 经验
- 33856 点
- 帖子
- 5311
- 精华
- 0
- 在线时间
- 3033 小时
- 注册时间
- 2011-4-17
- 最后登录
- 2022-7-29
|
沙发
songlinjl(真实交易用户)
发表于 2014-7-30 15:50:27
来自手机
- Getting nosy with testing
- Nose automatically discovers tests when fed with a package, a module, or a file.
- How to do it...
- With the following steps, we will explore how nose automatically finds test cases and runs them:
- Create a new file called recipe11.py in which to put all our code for this recipe.
- Create a class to test. For this recipe, we will use a shopping cart application that lets us load items and then calculate the bill.
- class ShoppingCart(object):
- def __init__(self):
- self.items = []
- def add(self, item, price):
- self.items.append(Item(item, price))
- return self
- def item(self, index):
- return self.items[index-1].item
- def price(self, index):
- return self.items[index-1].price
- def total(self, sales_tax):
- sum_price = sum([item.price for item in self.items])
- return sum_price*(1.0 + sales_tax/100.0)
- def __len__(self):
- return len(self.items)
- class Item(object):
- def __init__(self, item, price):
- self.item = item
- self.price = price
- Create a test case that exercises the various parts of the shopping cart application.
- import unittest
- class ShoppingCartTest(unittest.TestCase):
- def setUp(self):
- self.cart = ShoppingCart().add("tuna sandwich", 15.00)
- def test_length(self):
- self.assertEquals(1, len(self.cart))
- def test_item(self):
- self.assertEquals("tuna sandwich", self.cart.item(1))
- def test_price(self):
- self.assertEquals(15.00, self.cart.price(1))
- def test_total_with_sales_tax(self):
- self.assertAlmostEquals(16.39, \
- self.cart.total(9.25), 2)
- Use the command-line nosetests tool to run this recipe by filename and also by module.
复制代码
|
|