A new method to generate tests for deterministic implementations of deterministic finite state machines is proposed. The method is generic, taking a wide class of testing strategies as a parameter, and able to support both transition-oriented and fault-model-driven testing. For a given specification machine, it builds a graph encoding the given strategy and then generates a test by solving on the graph a generalization of the Rural Postman Problem. Only the first phase is domain specific and therefore precisely described in the paper. The second phase is considered a responsibility of graph specialists, though we provide guidelines for solving the most commonly encountered special case. The strategy encoding produced in the first phase is such that the second phase automatically handles, in an integrated manner, the following optimization concerns: absolute avoidance of forbidden transitions, maximum avoidance of transitions which are for some reason considered undesirable, subtest choice, subtest ordering and subtest connection, possibly with overlapping. The method accepts multi-criteria transition cost functions. If both its phases are executed precisely, it generates a test optimal with respect to the adopted strategy, and a strategy for generating absolutely optimal tests is also given. For the cases where the complexity of the first phase or of the resulting graph is problematic, guidelines for systematically conducting the phase in an approximate way are provided.