Walmart won a suit alleging it didn’t accommodate a truck driver who said his inability to comply with a random drug test should have been excused by his medical condition, a federal judge ruled.

Craig Beller was fired after Wal-Mart Stores East Inc. and a related company designated as a failed test his inability to provide a urine sample on demand in May 2016, the U.S. District Court for the Southern District of Ohio said March 28.

Under the Department...