Custom Test Support

pytest fixtures

class pyfrc.test_support.pytest_plugin.PyFrcPlugin(robot_class, robot_file, robot_path)[source]

Pytest plugin. Each documented member function name can be an argument to your test functions, and the data that these functions return will be passed to your test function.

control()[source]

A fixture that provides control over the robot

Return type:TestController
fake_time()[source]

A fixture that gives you control over the time your robot is using

Return type:FakeTime
hal_data()[source]

Provides access to a dict with all the device data about the robot

See also

For a listing of what the dict contains and some documentation, see https://github.com/robotpy/robotpy-wpilib/blob/master/hal-sim/hal_impl/data.py

robot()[source]

Your robot instance

robot_file()[source]

The absolute filename your robot code is started from

robot_path()[source]

The absolute directory that your robot code is located at

wpilib()[source]

The wpilib module. Provided for backwards compatibility

exception pyfrc.test_support.pytest_plugin.ThreadStillRunningError[source]

Controlling the robot’s state

class pyfrc.test_support.controller.TestController(fake_time_inst)[source]

This object is used to control the robot during unit tests. You do not need to create an instance of this, instead use the controller fixture.

To set a game specific message for autonomous mode, just set the ‘game_specific_message’ property of this object, and it will be setup correctly upon transition into autonomous mode.

get_mode()[source]

Returns the current mode that the robot is in

Returns:‘autonomous’, ‘teleop’, ‘test’, or ‘disabled’
run_test(controller=None)[source]

Call this to execute the robot code. Cannot be called more than once in a single test.

If the controller argument is a class, it will be constructed and the instance will be returned.

Parameters:controller – This can either be a function that takes a single argument, or a class that has an ‘on_step’ function. If it is a class, an instance will be created. Either the function or the on_step function will be called with a single parameter, which is the the current robot time. If None, an error will be signaled unless set_practice_match() has been called.
set_autonomous(enabled=True)[source]

Puts the robot in autonomous mode

set_operator_control(enabled=True)[source]

Puts the robot in operator control mode

set_practice_match()[source]

Call this function to enable a practice match. Must only be called before run_test() is called.

set_test_mode(enabled=True)[source]

Puts the robot in test mode (the robot mode, not related to unit testing)

class pyfrc.test_support.fake_time.FakeTime[source]

Keeps track of time for robot code being tested, and makes sure the DriverStation is notified that new packets are coming in.

Note

Do not create this object, your testing code can use this object to control time via the fake_time fixture

get()[source]
Returns:The current time for the robot
increment_new_packet()[source]

Increment time enough to where the new DriverStation packet comes in

increment_time_by(time)[source]

Increments time by some number of seconds

Parameters:time (float) – Number of seconds to increment time by
initialize()[source]

Initializes fake time

reset()[source]

Resets the fake time to zero, and sets the time limit to default

set_time_limit(time_limit)[source]

Sets the amount of time that a robot will run. When time is incremented past this time, a TestRanTooLong is thrown.

The default time limit is 500 seconds

Parameters:time_limit (float) – Number of seconds
exception pyfrc.test_support.fake_time.TestEnded[source]

This is thrown when the controller has been signaled to end the test

This exception inherits from BaseException, so if you want to catch it you must explicitly specify it, as a blanket except statement will not catch this exception.’

Generally, only internal pyfrc code needs to catch this

exception pyfrc.test_support.fake_time.TestFroze[source]

This happens when an infinite loop of some kind in one of your non-robot threads is detected.

exception pyfrc.test_support.fake_time.TestRanTooLong[source]

This is thrown when the time limit has expired

This exception inherits from BaseException, so if you want to catch it you must explicitly specify it, as a blanket except statement will not catch this exception.’

Generally, only internal pyfrc code needs to catch this