unittest单元测试框架总结_keep one's resolveY的博客-程序员秘密

技术标签: python自动化测试(页面+接口)  

转自:http://www.cnblogs.com/yufeihlf/p/5707929.html

 unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果。今天笔者就总结下如何使用unittest单元测试框架来进行WEB自动化测试。

目录

一、unittest模块的各个属性说明

二、使用unittest框架编写测试用例思路

三、使用unittest框架编写测试用例实例

 

一、unittest模块的各个属性说明

点击返回目录

    先来聊一聊unittest模块的各个属性,所谓知己知彼方能百战百胜,了解unittest的各个属性,对于后续编写用例有很大的帮助。

1.unittest的属性如下:

['BaseTestSuite', 'FunctionTestCase', 'SkipTest', 'TestCase', 'TestLoader', 'TestProgram', 'TestResult', 'TestSuite', 'TextTestResult', 'TextTestRunner', '_TextTestResult', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__unittest', 'case', 'defaultTestLoader', 'expectedFailure', 'findTestCases', 'getTestCaseNames', 'installHandler', 'loader', 'main', 'makeSuite', 'registerResult', 'removeHandler', 'removeResult', 'result', 'runner', 'signals', 'skip', 'skipIf', 'skipUnless', 'suite', 'util']

说明:

unittest.TestCase:TestCase类,所有测试用例类继承的基本类。

class BaiduTest(unittest.TestCase):

unittest.main():使用她可以方便的将一个单元测试模块变为可直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中以“test”命名开头的测试方法,并自动执行他们。执行方法的默认顺序是:根据ASCII码的顺序加载测试用例,数字与字母的顺序为:0-9,A-Z,a-z。所以以A开头的测试用例方法会优先执行,以a开头会后执行。

unittest.TestSuite():unittest框架的TestSuite()类是用来创建测试套件的。

unittest.TextTextRunner():unittest框架的TextTextRunner()类,通过该类下面的run()方法来运行suite所组装的测试用例,入参为suite测试套件。

unittest.defaultTestLoader(): defaultTestLoader()类,通过该类下面的discover()方法可自动更具测试目录start_dir匹配查找测试用例文件(test*.py),并将查找到的测试用例组装到测试套件,因此可以直接通过run()方法执行discover。用法如下:

discover=unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')

unittest.skip():装饰器,当运行用例时,有些用例可能不想执行等,可用装饰器暂时屏蔽该条测试用例。一种常见的用法就是比如说想调试某一个测试用例,想先屏蔽其他用例就可以用装饰器屏蔽。

@unittest.skip(reason): skip(reason)装饰器:无条件跳过装饰的测试,并说明跳过测试的原因。

@unittest.skipIf(reason): skipIf(condition,reason)装饰器:条件为真时,跳过装饰的测试,并说明跳过测试的原因。

@unittest.skipUnless(reason): skipUnless(condition,reason)装饰器:条件为假时,跳过装饰的测试,并说明跳过测试的原因。

@unittest.expectedFailure(): expectedFailure()测试标记为失败。

 

2.TestCase类的属性如下:

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addSkip', '_baseAssertEqual', '_classSetupFailed', '_deprecate', '_diffThreshold', '_formatMessage', '_getAssertEqualityFunc', '_truncateMessage', 'addCleanup', 'addTypeEqualityFunc', 'assertAlmostEqual', 'assertAlmostEquals', 'assertDictContainsSubset', 'assertDictEqual', 'assertEqual', 'assertEquals', 'assertFalse', 'assertGreater', 'assertGreaterEqual', 'assertIn', 'assertIs', 'assertIsInstance', 'assertIsNone', 'assertIsNot', 'assertIsNotNone', 'assertItemsEqual', 'assertLess', 'assertLessEqual', 'assertListEqual', 'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals', 'assertNotEqual', 'assertNotEquals', 'assertNotIn', 'assertNotIsInstance', 'assertNotRegexpMatches', 'assertRaises', 'assertRaisesRegexp', 'assertRegexpMatches', 'assertSequenceEqual', 'assertSetEqual', 'assertTrue', 'assertTupleEqual', 'assert_', 'countTestCases', 'debug', 'defaultTestResult', 'doCleanups', 'fail', 'failIf', 'failIfAlmostEqual', 'failIfEqual', 'failUnless', 'failUnlessAlmostEqual', 'failUnlessEqual', 'failUnlessRaises', 'failureException', 'id', 'longMessage', 'maxDiff', 'run', 'setUp', 'setUpClass', 'shortDescription', 'skipTest', 'tearDown', 'tearDownClass']

说明:

setUp():setUp()方法用于测试用例执行前的初始化工作。如测试用例中需要访问数据库,可以在setUp中建立数据库连接并进行初始化。如测试用例需要登录web,可以先实例化浏览器。

tearDown():tearDown()方法用于测试用例执行之后的善后工作。如关闭数据库连接。关闭浏览器。

assert*():一些断言方法:在执行测试用例的过程中,最终用例是否执行通过,是通过判断测试得到的实际结果和预期结果是否相等决定的。

assertEqual(a,b,[msg='测试失败时打印的信息']):断言a和b是否相等,相等则测试用例通过。

assertNotEqual(a,b,[msg='测试失败时打印的信息']):断言a和b是否相等,不相等则测试用例通过。

assertTrue(x,[msg='测试失败时打印的信息']):断言x是否True,是True则测试用例通过。

assertFalse(x,[msg='测试失败时打印的信息']):断言x是否False,是False则测试用例通过。

assertIs(a,b,[msg='测试失败时打印的信息']):断言a是否是b,是则测试用例通过。

assertNotIs(a,b,[msg='测试失败时打印的信息']):断言a是否是b,不是则测试用例通过。

assertIsNone(x,[msg='测试失败时打印的信息']):断言x是否None,是None则测试用例通过。

assertIsNotNone(x,[msg='测试失败时打印的信息']):断言x是否None,不是None则测试用例通过。

assertIn(a,b,[msg='测试失败时打印的信息']):断言a是否在b中,在b中则测试用例通过。

assertNotIn(a,b,[msg='测试失败时打印的信息']):断言a是否在b中,不在b中则测试用例通过。

assertIsInstance(a,b,[msg='测试失败时打印的信息']):断言a是是b的一个实例,是则测试用例通过。

assertNotIsInstance(a,b,[msg='测试失败时打印的信息']):断言a是是b的一个实例,不是则测试用例通过。

 

3.TestSuite类的属性如下:(组织用例时需要用到)

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addClassOrModuleLevelException', '_get_previous_module', '_handleClassSetUp', '_handleModuleFixture', '_handleModuleTearDown', '_tearDownPreviousClass', '_tests', 'addTest', 'addTests', 'countTestCases', 'debug', 'run']

说明:

addTest(): addTest()方法是将测试用例添加到测试套件中,如下方,是将test_baidu模块下的BaiduTest类下的test_baidu测试用例添加到测试套件。

suite = unittest.TestSuite()
suite.addTest(test_baidu.BaiduTest('test_baidu'))
 

4.TextTextRunner的属性如下:(组织用例时需要用到)

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_makeResult', 'buffer', 'descriptions', 'failfast', 'resultclass', 'run', 'stream', 'verbosity']

说明:

run(): run()方法是运行测试套件的测试用例,入参为suite测试套件。

runner = unittest.TextTestRunner()
runner.run(suite)

 

二、使用unittest框架编写测试用例思路

点击返回目录

设计基本思路如下:

复制代码
# coding=utf-8
#1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

#2.注释:包括记录创建时间,创建人,项目名称。
‘’’
Created on 2016-7-27
@author: Jennifer
Project:使用unittest框架编写测试用例思路
‘’’
#3.导入unittest模块
import unittest

#4.定义测试类,父类为unittest.TestCase。
#可继承unittest.TestCase的方法,如setUp和tearDown方法,不过此方法可以在子类重写,覆盖父类方法。
#可继承unittest.TestCase的各种断言方法。
class Test(unittest.TestCase):

#5.定义setUp()方法用于测试用例执行前的初始化工作。
#注意,所有类中方法的入参为self,定义方法的变量也要“self.变量”
#注意,输入的值为字符型的需要转为int型
def setUp(self):
self.number=raw_input(‘Enter a number:’)
self.number=int(self.number)

#6.定义测试用例,以“test_”开头命名的方法
#注意,方法的入参为self
#可使用unittest.TestCase类下面的各种断言方法用于对测试结果的判断
#可定义多个测试用例
#最重要的就是该部分
def test_case1(self):
print self.number
self.assertEqual(self.number,10,msg=‘Your input is not 10’)

</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">def</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> test_case2(self):
    </span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">print</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> self.number
    self.assertEqual(self.number,</span>20,msg=<span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">'</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">Your input is not 20</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">'</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)

@unittest.skip(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">'</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">暂时跳过用例3的测试</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">'</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">def</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> test_case3(self):
    </span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">print</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> self.number
    self.assertEqual(self.number,</span>30,msg=<span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">'</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">Your input is not 30</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">'</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)

#7.定义tearDown()方法用于测试用例执行之后的善后工作。
#注意,方法的入参为self
def tearDown(self):
print ‘Test over’

#8如果直接运行该文件(name__值为__main),则执行以下语句,常用于测试脚本是否能够正常运行
if name==‘main’:
#8.1执行测试用例方案一如下:
#unittest.main()方法会搜索该模块下所有以test开头的测试用例方法,并自动执行它们。
#执行顺序是命名顺序:先执行test_case1,再执行test_case2
unittest.main()

‘’’
#8.2执行测试用例方案二如下:
#8.2.1先构造测试集
#8.2.1.1实例化测试套件
suite=unittest.TestSuite()
#8.2.1.2将测试用例加载到测试套件中。
#执行顺序是安装加载顺序:先执行test_case2,再执行test_case1
suite.addTest(Test(‘test_case2’))
suite.addTest(Test(‘test_case1’))
#8.2.2执行测试用例
#8.2.2.1实例化TextTestRunner类
runner=unittest.TextTestRunner()
#8.2.2.2使用run()方法运行测试套件(即运行测试套件中的所有用例)
runner.run(suite)
’’’

‘’’
#8.3执行测试用例方案三如下:
#8.3.1构造测试集(简化了方案二中先要创建测试套件然后再依次加载测试用例)
#执行顺序同方案一:执行顺序是命名顺序:先执行test_case1,再执行test_case2
test_dir = ‘./’
discover = unittest.defaultTestLoader.discover(test_dir, pattern=‘test_*.py’)
#8.3.2执行测试用例
#8.3.2.1实例化TextTestRunner类
runner=unittest.TextTestRunner()
#8.3.2.2使用run()方法运行测试套件(即运行测试套件中的所有用例)
runner.run(discover)
‘’’

复制代码

使用方案一执行测试用例结果如下:

Enter a number:10
10
Test over
Enter a number:.10
Fs

Ran 3 tests in 6.092s

FAILED (failures=1, skipped=1)
10
Test over

因为先执行test_case1,再执行test_case2,所以第一次输入10时,执行通过,返回. 第二次输入10时,执行不通过,返回F,最终一个用例通过,一个用例失败,还有一个用例是直接跳过的(装饰器)。

 使用方案二执行测试用例结果如下:

Enter a number:10
10
Test over
Enter a number:F10
.

Ran 2 tests in 4.973s

FAILED (failures=1) 
10
Test over

因为先执行test_case2,再执行test_case1,所以第一次输入10时,执行不通过,返回F , 第二次输入10时,执行通过,返回. ,最终一个用例通过,一个用例失败。

使用方案三执行测试用例结果如下(执行测试用例顺序同方案一):

Enter a number:10
10
Test over
Enter a number:.10
Fs

Ran 3 tests in 6.092s

FAILED (failures=1, skipped=1)
10
Test over

因为先执行test_case1,再执行test_case2,所以第一次输入10时,执行通过,返回. 第二次输入10时,执行不通过,返回F,最终一个用例通过,一个用例失败,还有一个用例是直接跳过的(装饰器)。

 

三、使用unittest框架编写测试用例实例

点击返回目录

目录结构:

百度搜索测试用例Test Case:

复制代码
# coding=utf-8
'''
Created on 2016-7-22
@author: Jennifer
Project:登录百度测试用例
'''
from selenium import webdriver
import unittest, time

class BaiduTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30) #隐性等待时间为30秒
self.base_url = “https://www.baidu.com”

<span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">def</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> test_baidu(self):
    driver </span>=<span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> self.driver
    driver.get(self.base_url </span>+ <span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">/</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
    driver.find_element_by_id(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">kw</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">).clear()
    driver.find_element_by_id(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">kw</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span>).send_keys(<span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">unittest</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
    driver.find_element_by_id(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">su</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">).click()
    time.sleep(</span>3<span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
    title</span>=<span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">driver.title
    self.assertEqual(title, u</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">unittest_百度搜索</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">) 

</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">def</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> tearDown(self):
    self.driver.quit()

if name == “main”:
unittest.main()

复制代码

有道翻译测试用例Test Case:

复制代码
# coding=utf-8
'''
Created on 2016-7-22
@author: Jennifer
Project:使用有道翻译测试用例
'''
from selenium import webdriver
import unittest, time

class YoudaoTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30) #隐性等待时间为30秒
self.base_url = “http://www.youdao.com”

<span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">def</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> test_youdao(self):
    driver </span>=<span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> self.driver
    driver.get(self.base_url </span>+ <span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">/</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
    driver.find_element_by_id(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">translateContent</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">).clear()
    driver.find_element_by_id(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">translateContent</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span>).send_keys(u<span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">你好</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
    driver.find_element_by_id(</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">translateContent</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">).submit()
    time.sleep(</span>3<span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">)
    page_source</span>=<span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">driver.page_source
    self.assertIn( </span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">hello</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">"</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;">,page_source) 

</span><span style="font-family:'Courier New' !important;font-size:12px !important;line-height:1.5 !important;">def</span><span style="font-family:'Courier New' !important;color:#000000;font-size:12px !important;line-height:1.5 !important;"> tearDown(self):
    self.driver.quit()

if name == “main”:
unittest.main()

复制代码

web测试用例:通过测试套件TestSuite来组装多个测试用例。

复制代码
# coding=utf-8
'''
Created on 2016-7-26
@author: Jennifer
Project:编写Web测试用例
'''
import unittest
from test_case import test_baidu
from test_case import test_youdao

#构造测试集
suite = unittest.TestSuite()
suite.addTest(test_baidu.BaiduTest(‘test_baidu’))
suite.addTest(test_youdao.YoudaoTest(‘test_youdao’))

if name==‘main’:
#执行测试
runner = unittest.TextTestRunner()
runner.run(suite)

复制代码

测试结果:

 

说明:.代表用例执行通过,两个点表示两个用例执行通过。F表示用例执行不通过。


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_45433031/article/details/104956957

智能推荐

判断网络是否使用代理服务器_番薯大佬的博客-程序员秘密

直接上代码呗// 导入头文件#import &lt;SystemConfiguration/CaptiveNetwork.h&gt;// 代码实现- (BOOL)isUseProxy{ CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings(); const CFStringRef proxyCFstr = (...

POJ 3159 Candies 差分规划 + (SPFA | Dijkstra)_BranZhai的博客-程序员秘密

POJ 3159题意题目链接题解Dijkstra代码(经过堆优化)SPFA(用队列会超时, 须手动写栈)题意给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数&lt;= c 。最后求n 比 1 最多多多少糖果。题目链接POJ 3159题解首先,在读懂题意的情况下,仔细想一想,其实要使节点1与节点n之间的差最大,就需要每两个节点之间的糖果数相差最大。其中,同学为节点,允许相差的最大的糖果数是相应的节点之间的权值。

KITTI数据集data_object_velodyne中.bin文件转标准点云文件.pcd_平流层生物的博客-程序员秘密

KITTI数据集data_object_velodyne中.bin文件转标准点云文件.pcd最近开始学习自动驾驶领域中基于激光雷达的目标识别方向,自然而然的想到自动驾驶领域有名的数据集KITTI,想要去看一下雷达测到的数据和相机拍到的数据能不能对应上。打开文件,好家伙,创建者为了方便把雷达测到的数据全部转成了二进制bin文件????。在网上转了一圈,发现都是用c++或者python写的转换文件,还要附带安装一车的库(好不方便的说)。算了,自己动手,丰衣足食。从KITTI官网查readme文档,自己写

秒杀系统的两种设计方案_码农飞飞的博客-程序员秘密

秒杀系统面临的挑战秒杀活动和类似的抢购活动中,用户会在短时间内集中操作。后台系统可能在一瞬间面临平时几十倍甚至上百倍的并发请求。如果后台系统没有没有足够的冗余资源的话,系统可能被一瞬间搞瘫痪。如何应对短时间内的请求洪峰,让系统正常工作,是一个秒杀系统需要应对的严峻挑战。由于秒杀任务面临的请求是瞬时的,扩充机器数量是不合理的也不划算。如何在现有硬件条件下,应对秒杀活动的海量请求需要从业务流程方面进行优化。下面介绍两种秒杀系统解决问题的设计思路。1.分布式CDN边缘节点促销活动中客户端会不停的请

Python 关于 urllib.request.urlopen()的错误_Alex_ching的博客-程序员秘密

在刚开始学习python准备学习爬虫时在使用pycharm编译出现了错误这是源码:#!usr/bin/pythonimport urllib.requesturl = "http://www.baidu.com"get = urllib.request.urlopen(url).read()print(get)这个本来就很简单但是出现了以下错误:C:\Progr

Spring IOC-Spring扩展点大集合-第四篇_阿健2020的博客-程序员秘密

Spring是一个非常优秀的框架,其拓展点非常多,可以自定义很多功能。很多框架都采用了Spring的拓展点,例如:Dubbo,DisConf,Mybatis等。目前先梳理下Ioc部分的拓展点,文章部分内容参考:https://blog.gavinzh.com/2017/11/20/spring-develop-summary/,同时结合自己研读的源码,综合整理而来。此外,Aop部分的拓展点也很多...

随便推点

OpenCV——获得摄像头的帧流_嘿哈哈哈的博客-程序员秘密

VideoCapture类可以获得摄像头的帧流。对摄像头而言,需要传递摄像头的设备索引。下面的例子会捕获摄像头10秒的视频信息,并将其写入一个AVI文件中。但是当VideoCapture类所使用的终端不支持查询的这个属性时,会返回0。例如,get()。源代码import cv2cameraCapture = cv2.VideoCapture(0)fps = 30size = (in...

Matlab 绘制竖线(直线),固定组合图中子图位置, Matlab绘制小间距组合图方法,设置图片不显示直接保存_matlab画一条竖直线_ciciph的博客-程序员秘密

MAtlab 绘制竖线,组合图中子图位置确定, Matlab绘制小间距组合图方法,设置图片不显示直接保存一、 Matlab 绘制竖线 a1 = find(T == min(T((T - cut1 / 1e3) &gt; 0))); b1 = find(T == min(T((T - (cut1 + 250) / 1e3) &gt; 0))); data1 = StrainTemp(a1:b1); a = find(T == min(T((T - cut2 / 1e3) &gt;

Python简单实现基于VSM的余弦相似度计算_liuxiangke0210的博客-程序员秘密

原文链接:https://blog.csdn.net/eastmount/article/details/49898133  在知识图谱构建阶段的实体对齐和属性值决策、判断一篇文章是否是你喜欢的文章、比较两篇文章的相似性等实例中,都涉及到了向量空间模型(Vector Space Model,简称VSM)和余弦相似度计算相关知识。        这篇文章主要是先叙述VSM和余弦相似度相关理论知...

基于SteamVR_Unity_Toolkit自定义射线触发器简单结构_steamvr unity toolkit_VniciGino的博客-程序员秘密

环境:HtcVive,Unity,C#,商店的SteamVR_Unity_Toolkit插件目前htc交互比较主流的还是那个实体射线交互,工具包自带的三种:GetComponent().DestinationMarkerEnterGetComponent().DestinationMarkerExitGetComponent().DestinationMarkerSet工具包自带的三种交互使用起来不

关于SP3220EEY-L/TR_weixin_44218979的博客-程序员秘密

SP3220EEY-L/TR制造商:Exar无铅情况/RoHs:无铅/符合RoHs描述 :SP3220E Series 120 kbps 5.5 V RS-232 1 Driver / 1 Receiver Pair - TSSOP-16技术参考:电压-电源 3 V ~ 5.5 V封装/外壳 16-TSSOPFET类型 收发器协议 RS232驱动器/接收器数 1/1双工 全...

推荐文章

热门文章

相关标签