Skip to content

Commit

Permalink
Introduce pytest structure for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
eleurent committed Apr 17, 2018
1 parent 898c533 commit 4396881
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ _build
# Editor files
.idea/*

# Test files
.pytest_cache/*

# Outputs
**/out/*
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test=pytest
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

keywords='autonomous highway driving simulation environment reinforcement learning',
packages=find_packages(exclude=['docs', 'tests*']),
install_requires=['gym', 'numpy', 'pygame', 'jupyter', 'matplotlib', 'pandas'],
install_requires=['gym', 'numpy', 'pygame', 'jupyter', 'matplotlib', 'pandas', 'pytest-runner'],
tests_require=['pytest'],
extras_require={
'dev': ['scipy'],
},
Expand Down
File renamed without changes.
32 changes: 32 additions & 0 deletions tests/envs/test_gym.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from __future__ import division, print_function
import gym

import highway_env


def test_highway_step():
env = gym.make('highway-v0')

env.reset()
for i in range(3):
action = env.action_space.sample()
obs, reward, done, info = env.step(action)
env.close()

assert obs
assert reward
assert not done


def test_merge_step():
env = gym.make('highway-merge-v0')

env.reset()
for i in range(3):
action = env.action_space.sample()
obs, reward, done, info = env.step(action)
env.close()

assert obs
assert reward
assert not done
Empty file added tests/full/__init__.py
Empty file.
File renamed without changes.
68 changes: 68 additions & 0 deletions tests/full/generate_videos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import division, print_function

import gym
import highway_env

from highway_env.vehicle.behavior import IDMVehicle
from highway_env.agent.ttc_vi import TTCVIAgent
from highway_env.agent.mcts import MCTSAgent
from highway_env.wrappers.simulation import Simulation


def highway_ttcvi():
IDMVehicle.POLITENESS = 0
env = gym.make('highway-v0')
env.vehicle.position[0] -= 70
env.vehicle.position[1] = 3*4
env.vehicle.target_lane_index = 3
agent = TTCVIAgent(env)
sim = Simulation(env, agent, render_agent=False)

sim.step()
sim.render()
while not sim.done:
sim.step()
sim.close()


def highway_mcts():
IDMVehicle.POLITENESS = 0.2
env = gym.make('highway-v0')
env.vehicle.position[0] -= 70
env.vehicle.position[1] = 3*4
env.vehicle.target_lane_index = 3
agent = MCTSAgent(env, temperature=300, iterations=100)
sim = Simulation(env, agent)

sim.step()
sim.render()
while not sim.done:
sim.step()
sim.close()


def merge():
IDMVehicle.POLITENESS = 0
env = gym.make('highway-merge-v0')
agent = MCTSAgent(env,
prior_policy=MCTSAgent.fast_policy,
rollout_policy=MCTSAgent.idle_policy,
iterations=75,
temperature=200,
assume_vehicle_type=None)
sim = Simulation(env, agent, render_agent=False)

sim.step()
sim.render()
while not sim.done:
sim.step()
sim.close()


if __name__ == '__main__':
for _ in range(10):
highway_mcts()
for _ in range(10):
highway_ttcvi()
for _ in range(0):
merge()
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
19 changes: 0 additions & 19 deletions tests/highway_gym.py

This file was deleted.

10 changes: 10 additions & 0 deletions tests/road/test_road.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import division, print_function
import pytest

from highway_env.road.road import Road


def test_random():
r = Road.create_random_road(lanes_count=6, lane_width=4.0, vehicles_count=42)
assert len(r.lanes) == 6
assert len(r.vehicles) == 42
49 changes: 49 additions & 0 deletions tests/vehicle/test_dynamics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import division, print_function
import pytest

from highway_env.road.road import Road
from highway_env.vehicle.dynamics import Vehicle

FPS = 15


def test_step():
v = Vehicle(road=None, position=[0, 0], velocity=20, heading=0)
for _ in range(2*FPS):
v.step(dt=1/FPS)
assert v.position[0] == pytest.approx(40)
assert v.position[1] == pytest.approx(0)
assert v.velocity == pytest.approx(20)
assert v.heading == pytest.approx(0)


def test_act():
v = Vehicle(road=None, position=[0, 0], velocity=20, heading=0)
v.act({'acceleration': 1, 'steering': 0})
for _ in range(1 * FPS):
v.step(dt=1/FPS)
assert v.velocity == pytest.approx(21)

v.act({'acceleration': 0, 'steering': 0.5})
for _ in range(1 * FPS):
v.step(dt=1/FPS)
assert v.velocity == pytest.approx(21)
assert v.position[1] > 0


def test_brake():
v = Vehicle(road=None, position=[0, 0], velocity=20, heading=0)
for _ in range(10 * FPS):
v.act({'acceleration': min(max(-1*v.velocity, -6), 6), 'steering': 0})
v.step(dt=1/FPS)
assert v.velocity == pytest.approx(0, abs=0.01)


def test_front():
r = Road.create_random_road(lanes_count=1, lane_width=0, vehicles_count=0)
v1 = Vehicle(road=r, position=[0, 0], velocity=20)
v2 = Vehicle(road=r, position=[10, 0], velocity=10)
r.vehicles.extend([v1, v2])

assert v1.lane_distance_to(v2) == pytest.approx(10)
assert v2.lane_distance_to(v1) == pytest.approx(-10)

0 comments on commit 4396881

Please sign in to comment.