Best Python code snippet using tox_python
unit_test.py
Source:unit_test.py
1import unittest2from simulation.modele.robot import Robot3from simulation.modele.environment import Environment4from simulation.modele.obstacle import Obstacle5from math import cos,sin,pi6from time import time7# Commande pour lancer les unit_test depuis /srcs :8# python3 -m unittest tests.unit_test -v9class TestRobot(unittest.TestCase):10 def setUp(self):11 self.r1=Robot(2,3)12 self.r2=Robot(10,2)13 self.obs=Obstacle(2,3,2,1)14 self.env=Environment(3,4)15 self.env2=Environment(800,300)16 self.rob = Robot(300,200)17 self.env2.addRob(self.rob)18 19 def test_changeDir(self):20 self.r1.changeDir(5)21 self.assertEqual(self.r1.dir,5)22 self.r1.changeDir(-5)23 self.assertEqual(self.r1.dir,-5)24 self.r2.changeDir(10)25 self.assertEqual(self.r2.dir,10)26 self.r2.changeDir(-15)27 self.assertEqual(self.r2.dir, -15)28 def test_deplacerRobot(self):29 dir = self.r1.dir * pi / 180 #conversion des degrés en radians30 self.r1.deplacerRobot(12)31 self.assertAlmostEqual(self.r1.positionX, self.r1.positionX + 12 * cos(dir), 7)32 self.assertAlmostEqual(self.r1.positionY, self.r1.positionY - 12 * cos(dir), 7)33 def test_move(self):34 self.r1.move(8,9)35 self.assertEqual(self.r1.positionX,8)36 self.assertEqual(self.r1.positionY,9)37 def test_changeSpeed(self):38 first_speed=self.r1.speedLeftWheel39 self.r1.changeSpeed(50)40 self.assertEqual(self.r1.speedLeftWheel,(first_speed+50))41 def test_detecteCollision(self):42 test_obj = Obstacle(10,50,100,30)43 self.r1.detecteCollision(test_obj)44 def test_changeWheelMode(self):45 self.r1.changeWheelMode(2)46 self.assertEqual(self.r1.wheelMode,2)47 def test_updateDir(self):48 first_direction=self.r1.dir49 distance = (2 * pi * self.r1.radius_of_wheels ) * (3*pi/ 360) #distance parcourue à partir de l'angle effectué par les roues50 r = self.r1.half_dist_between_wheels #rayon / DOIT REFLETER LA DISTANCE ENTRE LE CENTRE DU ROBOT ET L'UNE DE SES ROUES !!!51 angle_rotated_by_robot = (360 * distance) / (2 * pi * r)52 self.r1.updateDir(3*pi)53 self.assertAlmostEqual(self.r1.dir, (first_direction+angle_rotated_by_robot))54 def test_changeSpeed(self):55 first_speed=self.r1.speedLeftWheel56 self.r1.changeSpeed(50)57 self.assertEqual(self.r1.speedLeftWheel,(first_speed+50))58 def test_deplacerRobot(self):59 first_positionX = self.r1.positionX60 first_positionY = self.r1.positionY61 self.r1.deplacerRobot(70)62 dir = self.r1.dir * pi / 180 #conversion des degrés en radians63 distance = (2 * pi * self.r1.radius_of_wheels) * (70 / 360) #distance parcourue à partir de l'angle effectué par les roues64 dx = distance * cos(dir)65 dy = distance * sin(dir)66 self.assertEqual(self.r1.positionX,first_positionX+dx)67 self.assertEqual(self.r1.positionY,first_positionY-dy)68 def test_update(self):69 """ verifie si la distance entre le centre du robot et l'une de ses roues est mise a jour70 """71 current_time = time()72 elapsed_time = current_time - self.r1.last_time73 angle_rotated = self.r1.dir * elapsed_time # Angle effectué par les roues = Vitesse (Degrés Par Seconde) * Temps (Secondes)74 first_angle_rotated_left_wheel=self.r1.angle_rotated_left_wheel75 first_angle_rotated_right_wheel=self.r1.angle_rotated_right_wheel76 self.r1.update()77 if self.r1.wheelMode == 1: #roues en mode 1 pour avancer/reculer78 self.assertFalse(self.r1.angle_rotated_left_wheel ,( first_angle_rotated_left_wheel+angle_rotated))79 self.assertFalse(self.r1.angle_rotated_right_wheel ,(first_angle_rotated_right_wheel+angle_rotated))80 elif self.wheelMode == 2: #roues en mode 2 pour tourner81 self.assertFalse(self.r1.angle_rotated_left_wheel ,( first_angle_rotated_left_wheel-angle_rotated))82 self.assertFalse(self.r1.angle_rotated_right_wheel ,( first_angle_rotated_right_wheel+angle_rotated))83 self.r1.last_time = time()84 85 def test_move(self):86 self.r1.move(8,9)87 self.assertEqual(self.r1.positionX,8)88 self.assertEqual(self.r1.positionY,9)89 def test_scalairevectoriel(self):90 resultat=self.r1.scalairevectoriel(2,5,8,3,1,7,4,9)91 k=(2*3-5*8)*(1*9-4*7)92 self.assertAlmostEqual(k,resultat)93 def test_detecteCollision(self):94 k=self.r1.detecteCollision(self.obs)95 self.assertEqual(k,True)96 def test_is_outside_of_the_environment(self):97 k=self.r1.is_outside_of_the_environment(2,3,self.env)98 self.assertEqual(k,False)99 def test_is_inside_an_obstacle_in_the_environment(self):100 self.env.addObject(self.obs)101 k=self.r1.is_inside_an_obstacle_in_the_environment(2, 3, self.env)102 self.assertEqual(k,False)103 def test_getDistance(self):104 k=self.rob.getDistance(self.env2)105 self.assertEqual(k,201)106 if __name__ == '__main__':107 unittest.main()108class TestEnvironment(unittest.TestCase):109 def setUp(self):110 self.env1=Environment(500,300)111 self.env2=Environment(800,200)112 self.rob=Robot(2,3)113 self.obs=Obstacle(9,7,20,13)114 def test_addObject(self):115 test_obj = Obstacle(10,50,100,30)116 self.env1.addObject(test_obj)117 self.assertIn(test_obj, self.env1.objects)118 def test_removeObject(self):119 test_obj = Obstacle(10,50,100,30)120 self.env1.addObject(test_obj)121 self.assertIn(test_obj, self.env1.objects)122 self.env1.removeObject(test_obj)123 self.assertNotIn(test_obj, self.env1.objects)124 def test_addRob(self):125 self.env1.addRob(self.rob)126 self.assertIn(self.rob,self.env1.objects)127 128 def test_testCollisionRob(self):129 #c : bool130 c = self.env1.testCollisionRob(self.rob) # on s'assure qu'il y a un conflit entre la position du robot et les objets de l'environment131 self.assertTrue(c)132 133 def test_testCollisionObs(self):134 #c : bool135 c = self.env1.testCollisionObs(self.obs) # on s'assure qu'il y a un conflit entre la position du robot et les objets de l'environment136 self.assertTrue(c)137 138 def test_addObject(self):139 # addObject prend un objet de type Environment puis l'ajoute à l'environnement.140 test_obj = Obstacle(5,25,102,30)141 self.env1.addObject(test_obj) 142 self.assertIn(test_obj,self.env1.objects) # un environement est un ensemble d'objets143 144 145 if __name__ == '__main__':146 unittest.main()147 148class TestObstacle(unittest.TestCase):149 def setUp(self):150 self.ob1=Obstacle()151 self.ob2=Obstacle()152 if __name__ == '__main__':...
DMtest.py
Source:DMtest.py
1from lib.DirectoryManager import DirManager2import unittest3import random4import string5s = 'success'6f = 'failure'7class DirManagementTest(unittest.TestCase):8 def testNonExistantFolder(self):9 b = DirManager('TestUser')10 res = b.chd('feri')11 self.assertTrue(res.startswith(f))12 def test_EscapeHome(self):13 b = DirManager('TestUser')14 res = b.chd('..')15 self.assertTrue(res.startswith(f))16 def test_ChangeDir(self):17 b = DirManager('TestUser')18 res = b.chd('test')19 self.assertTrue(res.startswith(s))20 def test_EscapeDir(self):21 b = DirManager('TestUser')22 res = b.chd('test/../../..')23 self.assertTrue(res.startswith(f))24 def test_Pwd(self):25 b = DirManager('TestUser')26 res = b.pwd()27 self.assertEqual(s+'\n~/', res)28 def test_Pwd2(self):29 b = DirManager('TestUser')30 b.chd('test')31 res = b.pwd()32 self.assertTrue(res.endswith('test'))33 def test_Lst1(self):34 b = DirManager('TestUser')35 res = b.lst()36 self.assertTrue(res.startswith('success'))37 self.assertFalse(res.find('fer.txt') is -1)38 self.assertFalse(res.find('test') is -1)39 def test_Del1(self):40 b = DirManager('testuser')41 res = b.delete('random.txt')42 self.assertEqual(s, res)43 res = b.delete('random.txt')44 self.assertFalse(res.startswith(s))45 def test_Mkd1(self):46 b = DirManager('TestUser')47 res = b.mkd('test')48 self.assertFalse(res.startswith('success'))49 def test_Mkd2(self):50 b = DirManager('TestUser')51 dirname = ''.join(random.choice(string.ascii_letters)52 for i in range(10))53 res = b.mkd(dirname)...
test_tools.py
Source:test_tools.py
...22 assert (res[1] - res[0] - 0.01e-6) < 1e-923 # Exception24 with pytest.raises(ValueError):25 uu.hysteresis_values(-1, 1, 0.3)26def test_changedir(tmp_path):27 with uu.changedir(tmp_path):28 with open("test.txt", "wt", encoding="utf-8") as f:29 f.write("")30 assert (tmp_path / "test.txt").exists()...
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!!