-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce pytest structure for unit testing
- Loading branch information
Showing
17 changed files
with
166 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,8 @@ _build | |
# Editor files | ||
.idea/* | ||
|
||
# Test files | ||
.pytest_cache/* | ||
|
||
# Outputs | ||
**/out/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[aliases] | ||
test=pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |