TRUNCATE in transaction: dead lock
TRUNCATE should be used only when it is certain that no other session is using the table at the same time. Otherwise DELETE should be used. It's slower (if the number of rows is significant), but it's designed to support concurrent access within the rules of the current isolation mode. 

The doc says:


  TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates
  on, which blocks all other concurrent operations on the table. When
  RESTART IDENTITY is specified, any sequences that are to be restarted
  are likewise locked exclusively. If concurrent access to a table is
  required, then the DELETE command should be used instead.


If DELETE's slowness is not acceptable, to avoid the deadlock with 100% certainty, you'd need to put the entire sequence into a critical section, both in the unit test and in the cron job. A critical section is entered by requesting an explicit lock before any other operation, such as in:

BEGIN
 SELECT pg_advisory_xact_lock(1); -- (1 is arbitrary: choose any value reserved for this purpose by your apps)
 TRUNCATE ...;
 TRUNCATE ...;
 ...processing...
ROLLBACK;