Best Python code snippet using fMBT_python
test_attr.py
Source:test_attr.py
1# vim: set et sw=4 sts=4 fileencoding=utf-8:2#3# Python camera library for the Rasperry-Pi camera module4# Copyright (c) 2013-2015 Dave Jones <dave@waveform.org.uk>5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are met:8#9# * Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# * Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14# * Neither the name of the copyright holder nor the15# names of its contributors may be used to endorse or promote products16# derived from this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28# POSSIBILITY OF SUCH DAMAGE.29from __future__ import (30 unicode_literals,31 print_function,32 division,33 absolute_import,34 )35# Make Py2's str equivalent to Py3's36str = type('')37import picamera38from picamera.color import Color39import pytest40import time41from fractions import Fraction42from decimal import Decimal43def numeric_attr(camera, attr, value_min, value_max, step=1):44 save_value = getattr(camera, attr)45 try:46 for value in range(value_min, value_max + 1, step):47 setattr(camera, attr, value)48 assert value == getattr(camera, attr)49 with pytest.raises(picamera.PiCameraError):50 setattr(camera, attr, value_min - 1)51 with pytest.raises(picamera.PiCameraError):52 setattr(camera, attr, value_max + 1)53 finally:54 setattr(camera, attr, save_value)55def keyword_attr(camera, attr, values):56 save_value = getattr(camera, attr)57 try:58 for value in values:59 setattr(camera, attr, value)60 assert value == getattr(camera, attr)61 with pytest.raises(picamera.PiCameraError):62 setattr(camera, attr, 'foobar')63 finally:64 setattr(camera, attr, save_value)65def boolean_attr(camera, attr):66 save_value = getattr(camera, attr)67 try:68 setattr(camera, attr, False)69 assert not getattr(camera, attr)70 setattr(camera, attr, True)71 assert getattr(camera, attr)72 finally:73 setattr(camera, attr, save_value)74def test_analog_gain(camera, previewing):75 # Just test the read-only property returns something sensible76 assert 0.0 <= camera.analog_gain <= 8.077def test_annotate_text(camera, previewing):78 save_value = camera.annotate_text79 try:80 camera.annotate_text = ''81 assert camera.annotate_text == ''82 camera.annotate_text = 'foo'83 assert camera.annotate_text == u'foo'84 camera.annotate_text = 'foo bar baz quux xyzzy'85 assert camera.annotate_text == u'foo bar baz quux xyzzy'86 with pytest.raises(picamera.PiCameraValueError):87 camera.annotate_text = ('abcd' * 64) + 'a'88 with pytest.raises(picamera.PiCameraValueError):89 camera.annotate_text = 'Oh lá lá'90 finally:91 camera.annotate_text = save_value92def test_annotate_text_size(camera, previewing):93 numeric_attr(camera, 'annotate_text_size', 6, 160)94def test_annotate_foreground(camera, previewing):95 save_value = camera.annotate_foreground96 try:97 camera.annotate_foreground = Color('black')98 camera.annotate_foreground = Color('white')99 camera.annotate_foreground = Color.from_yuv(0.5, 0, 0)100 with pytest.raises(picamera.PiCameraValueError):101 camera.annotate_foreground = 'white'102 with pytest.raises(picamera.PiCameraValueError):103 camera.annotate_foreground = 0104 with pytest.raises(picamera.PiCameraValueError):105 camera.annotate_foreground = None106 finally:107 camera.annotate_foreground = save_value108def test_annotate_background(camera, previewing):109 save_value = camera.annotate_background110 try:111 camera.annotate_background = Color('black')112 camera.annotate_background = Color('white')113 camera.annotate_background = Color(128, 128, 0)114 with pytest.raises(picamera.PiCameraValueError):115 camera.annotate_background = 'black'116 with pytest.raises(picamera.PiCameraValueError):117 camera.annotate_background = 0118 camera.annotate_background = None119 finally:120 camera.annotate_background = save_value121def test_annotate_frame_num(camera, previewing):122 boolean_attr(camera, 'annotate_frame_num')123def test_awb_mode(camera, previewing):124 keyword_attr(camera, 'awb_mode', camera.AWB_MODES)125def test_awb_gains(camera, previewing):126 def check_gains(red, blue):127 # The camera needs some time to let the AWB gains adjust128 time.sleep(0.4)129 # The gains we get back aren't absolutely precise, but they're130 # close (+/- 0.05)131 r, b = camera.awb_gains132 assert red - 0.05 <= r <= red + 0.05133 assert blue - 0.05 <= b <= blue + 0.05134 save_mode = camera.awb_mode135 try:136 # Can't use numeric_attr here as awb_mode is a (red, blue) tuple137 camera.awb_mode = 'off'138 for i in range (1, 9):139 camera.awb_gains = i140 check_gains(i, i)141 camera.awb_gains = 1.5142 check_gains(1.5, 1.5)143 camera.awb_gains = (0.5, 0.5)144 check_gains(0.5, 0.5)145 camera.awb_gains = (Fraction(16, 10), 1.9)146 check_gains(1.6, 1.9)147 with pytest.raises(picamera.PiCameraError):148 camera.awb_gains = Fraction(20, 1)149 finally:150 camera.awb_mode = save_mode151def test_brightness(camera, previewing):152 numeric_attr(camera, 'brightness', 0, 100)153def test_color_effects(camera, previewing):154 save_value = camera.color_effects155 try:156 camera.color_effects = None157 assert camera.color_effects is None158 camera.color_effects = (128, 128)159 assert camera.color_effects == (128, 128)160 camera.color_effects = (0, 255)161 assert camera.color_effects == (0, 255)162 camera.color_effects = (255, 0)163 assert camera.color_effects == (255, 0)164 with pytest.raises(picamera.PiCameraError):165 camera.color_effects = (-1, -1)166 with pytest.raises(picamera.PiCameraError):167 camera.color_effects = (0, 300)168 finally:169 camera.color_effects = save_value170def test_contrast(camera, previewing):171 numeric_attr(camera, 'contrast', -100, 100)172def test_digital_gain(camera, previewing):173 # Just test the read-only property returns something sensible174 assert 0.0 <= camera.digital_gain <= 8.0175def test_exposure_compensation(camera, previewing):176 numeric_attr(camera, 'exposure_compensation', -25, 25)177def test_exposure_mode(camera, previewing):178 keyword_attr(camera, 'exposure_mode', camera.EXPOSURE_MODES)179def test_flash_mode(camera, previewing):180 keyword_attr(camera, 'flash_mode', camera.FLASH_MODES)181def test_image_effects1(camera, previewing):182 valid_combinations = {183 'solarize': [184 (False, 128, 128, 128, 0),185 (True, 128, 128, 128, 0),186 (False, 16, 192, 0, 0),187 (128, 128, 128, 0),188 0,189 ],190 'colorbalance': [191 (0, 1, 1, 1, 0, 0),192 (0, 0.5, 0.5, 0.5),193 (0.451, 1, 1),194 (0, 1.0, 0.5, 0.75, -64, 64),195 ],196 'colorpoint': [0, (1,), (2,), 3],197 'colorswap': [0, (1,)],198 'posterise': [2, (30,), 16],199 'blur': [1, (2,)],200 'film': [(0, 0, 0), (50, 128, 128)],201 'watercolor': [(), (128, 128)],202 }203 try:204 for effect in camera.IMAGE_EFFECTS:205 camera.image_effect = effect206 assert camera.image_effect_params is None207 if effect in valid_combinations:208 for params in valid_combinations[effect]:209 camera.image_effect_params = params210 assert camera.image_effect_params == params211 finally:212 camera.image_effect = 'none'213def test_image_effects2(camera, previewing):214 invalid_combinations = {215 'solarize': [(3, 3, 3), ()],216 'colorpoint': [(1, 1), ()],217 'colorbalance': [(1,), False, ()],218 'colorswap': [(1, 1), ()],219 'posterise': [(1, 1), ()],220 'blur': [(1, 1), ()],221 'film': [(1, 1), (), (12, 2, 3, 4)],222 'watercolor': [1, (1, 2, 3)],223 }224 try:225 for effect, params_sets in invalid_combinations.items():226 camera.image_effect = effect227 for params in params_sets:228 with pytest.raises(picamera.PiCameraValueError):229 camera.image_effect_params = params230 finally:231 camera.image_effect = 'none'232def test_drc_strength(camera, previewing):233 keyword_attr(camera, 'drc_strength', camera.DRC_STRENGTHS)234def test_meter_mode(camera, previewing):235 keyword_attr(camera, 'meter_mode', camera.METER_MODES)236def test_rotation(camera, previewing):237 save_value = camera.rotation238 try:239 for value in range(0, 360):240 camera.rotation = value241 assert camera.rotation == [0, 90, 180, 270][value // 90]242 camera.rotation = 360243 assert camera.rotation == 0244 finally:245 camera.rotation = save_value246def test_saturation(camera, previewing):247 numeric_attr(camera, 'saturation', -100, 100)248def test_sharpness(camera, previewing):249 numeric_attr(camera, 'sharpness', -100, 100)250def test_iso(camera, previewing):251 numeric_attr(camera, 'iso', 0, 1600)252def test_video_stabilization(camera, previewing):253 boolean_attr(camera, 'video_stabilization')254def test_video_denoise(camera, previewing):255 boolean_attr(camera, 'video_denoise')256def test_image_denoise(camera, previewing):257 boolean_attr(camera, 'image_denoise')258def test_still_stats(camera, previewing):259 boolean_attr(camera, 'still_stats')260def test_hflip(camera, previewing):261 boolean_attr(camera, 'hflip')262def test_vflip(camera, previewing):263 boolean_attr(camera, 'vflip')264def test_shutter_speed(camera, previewing):265 # Shutter speed is now clamped by frame-rate; set frame-rate to something266 # nice and low to enable the test to run correctly267 save_framerate = camera.framerate268 camera.framerate = 1269 try:270 # When setting shutter speed manually, ensure the actual shutter speed271 # is within 50usec of the specified amount (the value+1 accounts for272 # a rounding error)273 for value in range(0, 700000, 50):274 camera.shutter_speed = value275 assert (value - 50) <= camera.shutter_speed <= (value + 1)276 # Test the shutter speed clamping by framerate277 camera.framerate = 30278 assert 33000 <= camera.shutter_speed <= 33333279 finally:280 camera.framerate = save_framerate281 camera.shutter_speed = 0282def test_zoom(camera, previewing):283 save_zoom = camera.zoom284 try:285 camera.zoom = (0.0, 0.0, 1.0, 1.0)286 assert camera.zoom == (0.0, 0.0, 1.0, 1.0)287 camera.zoom = (0.2, 0.2, 0.6, 0.6)288 assert camera.zoom == (0.2, 0.2, 0.6, 0.6)289 camera.zoom = (0.1, 0.1, 0.8, 0.8)290 # 0.1 doesn't quite make the round trip...291 assert camera.zoom == (int(0.1*65535.0)/65535.0, int(0.1*65535.0)/65535.0, 0.8, 0.8)292 finally:293 camera.zoom = save_zoom294# XXX The preview properties work, but don't return correct values unless the295# preview is actually running; if this isn't expected behaviour then we should296# xfail these tests instead of simply testing for previewing...297def test_preview_alpha(camera, previewing):298 if previewing:299 numeric_attr(camera.preview, 'alpha', 0, 255)300def test_preview_layer(camera, previewing):301 if previewing:302 numeric_attr(camera.preview, 'layer', 0, 255)303def test_preview_fullscreen(camera, previewing):304 if previewing:305 boolean_attr(camera.preview, 'fullscreen')306def test_preview_window(camera, previewing):307 if previewing:308 camera.preview.window = (0, 0, 320, 240)309 assert camera.preview.window == (0, 0, 320, 240)310 camera.preview.window = (1280-320, 720-240, 320, 240)311 assert camera.preview.window == (1280-320, 720-240, 320, 240)312 camera.preview.window = (0, 0, 640, 360)313 assert camera.preview.window == (0, 0, 640, 360)314 camera.preview.window = (0, 720-360, 640, 360)315 assert camera.preview.window == (0, 720-360, 640, 360)316 camera.preview.window = (1280-640, 0, 640, 360)317 assert camera.preview.window == (1280-640, 0, 640, 360)318 camera.preview.window = (1280-640, 720-360, 640, 360)319 assert camera.preview.window == (1280-640, 720-360, 640, 360)320 camera.preview.window = (0, 0, 1920, 1080)321 assert camera.preview.window == (0, 0, 1920, 1080)322def test_preview_resolution(camera, previewing):323 if previewing:324 save_resolution = camera.resolution325 try:326 camera.resolution = (640, 480)327 assert camera.preview.resolution is None328 camera.preview.resolution = (320, 240)329 assert camera.preview.resolution == (320, 240)330 assert camera._camera.outputs[0].framesize == (320, 240)331 assert camera._camera.outputs[2].framesize == (640, 480)332 camera.resolution = (320, 240)333 assert camera.preview.resolution is None334 assert camera._camera.outputs[0].framesize == (320, 240)335 assert camera._camera.outputs[2].framesize == (320, 240)336 camera.resolution = (1280, 720)337 assert camera.resolution == (1280, 720)338 assert camera.preview.resolution is None339 assert camera._camera.outputs[0].framesize == (1280, 720)340 assert camera._camera.outputs[2].framesize == (1280, 720)341 with pytest.raises(picamera.PiCameraValueError):342 camera.preview.resolution = (1281, 720)343 with pytest.raises(picamera.PiCameraValueError):344 camera.preview.resolution = (1280, 721)345 finally:346 camera.resolution = save_resolution347def test_preview_rotation(camera, previewing):348 if previewing:349 save_value = camera.preview.rotation350 try:351 for value in range(0, 360):352 camera.preview.rotation = value353 assert camera.preview.rotation == [0, 90, 180, 270][value // 90]354 camera.preview.rotation = 360355 assert camera.preview.rotation == 0356 finally:357 camera.preview.rotation = save_value358def test_preview_vflip(camera, previewing):359 if previewing:360 boolean_attr(camera.preview, 'vflip')361def test_preview_hflip(camera, previewing):362 if previewing:363 boolean_attr(camera.preview, 'hflip')364def test_sensor_mode(camera, previewing):365 save_mode = camera.sensor_mode366 try:367 for mode in range(8):368 camera.sensor_mode = mode369 assert camera.sensor_mode == mode370 with pytest.raises(picamera.PiCameraError):371 camera.sensor_mode = 10372 finally:373 camera.sensor_mode = save_mode374def test_framerate_delta(camera, previewing):375 for num in range(-10, 11):376 camera.framerate_delta = num / 10377 assert Fraction(num, 10) - Fraction(1, 256) <= camera.framerate_delta <= Fraction(num, 10) + Fraction(1, 256)378def test_framerate(camera, previewing):379 save_framerate = camera.framerate380 try:381 assert len(camera.framerate) == 2382 camera.framerate = (30, 1)383 n, d = camera.framerate384 assert n/d == 30385 camera.framerate = (15, 1)386 n, d = camera.framerate387 assert n/d == 15388 camera.framerate = 30389 n, d = camera.framerate390 assert n/d == 30391 camera.framerate = 15.0392 n, d = camera.framerate393 assert n/d == 15394 camera.framerate = Fraction(30, 2)395 n, d = camera.framerate396 assert n/d == 15397 camera.framerate = Decimal(30)398 n, d = camera.framerate399 assert n/d == 30400 camera.framerate = 60401 n, d = camera.framerate402 assert n/d == 60403 camera.framerate = 90404 n, d = camera.framerate405 assert n/d == 90406 with pytest.raises(picamera.PiCameraError):407 camera.framerate = (30, 0)408 with pytest.raises(picamera.PiCameraError):409 camera.framerate = -1410 with pytest.raises(picamera.PiCameraError):411 camera.framerate = 200412 finally:413 camera.framerate = save_framerate414def test_resolution(camera, previewing):415 save_resolution = camera.resolution416 try:417 # Test setting some regular resolutions418 camera.resolution = (320, 240)419 assert camera.resolution == (320, 240)420 assert camera._camera.outputs[2].framesize == (320, 240)421 camera.resolution = (640, 480)422 assert camera.resolution == (640, 480)423 assert camera._camera.outputs[2].framesize == (640, 480)424 camera.resolution = (1280, 720)425 assert camera.resolution == (1280, 720)426 assert camera._camera.outputs[2].framesize == (1280, 720)427 camera.resolution = (1920, 1080)428 assert camera.resolution == (1920, 1080)429 # Camera's vertical resolution is always a multiple of 16, and430 # horizontal is a multiple of 32, hence the difference in the video431 # formats here and below432 assert camera._camera.outputs[2]._port[0].format[0].es[0].video.width == 1920433 assert camera._camera.outputs[2]._port[0].format[0].es[0].video.height == 1088434 camera.resolution = (2592, 1944)435 assert camera.resolution == (2592, 1944)436 assert camera._camera.outputs[2]._port[0].format[0].es[0].video.width == 2592437 assert camera._camera.outputs[2]._port[0].format[0].es[0].video.height == 1952438 # Test some irregular resolutions439 camera.resolution = (100, 100)440 assert camera.resolution == (100, 100)441 assert camera._camera.outputs[2]._port[0].format[0].es[0].video.width == 128442 assert camera._camera.outputs[2]._port[0].format[0].es[0].video.height == 112443 # Anything below 16,16 will fail (because the camera's vertical444 # resolution works in increments of 16)445 with pytest.raises(picamera.PiCameraError):446 camera.resolution = (0, 0)447 with pytest.raises(picamera.PiCameraError):448 camera.resolution = (15, 15)449 finally:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!