This is false for spaces.
Let $X = S^0, Y = S^1$, and $f:X \to Y$ be the trivial map. Then $Z = Cf$ is $S^1 \vee S^1$. Then $[X,Y]$ is trivial, so then the the truth of this statement would imply: If you have a map $g: Z \to Z$ which is the identity on the first circle, and such that the induced map $S^1 \to S^1$ after collapsing the first circle is homotopic to the identity, then $g$ is a homotopy equivalence.
The map $g$ is a based map from a wedge of two circles to itself, which has fundamental group $F$, the free group on two generators, with generators $x, y$ corresponding to the two circle factors. A self-map is determined up to homotopy by a pair of elements in $x', y' \in F$. The condition that the first circle is mapped by the identity says $x = x'$, and the condition that the induced map on quotients is homotopic to the identity says that the image of $y'$ in $F/\langle x \rangle$ is the same as the image of $y$.
So: Suppose you have a pair of elements $x$ and $y'$ in $F = \langle x,y \rangle$ such that $y \equiv y'$ in $F/\langle x \rangle$. Then do $x$ and $y'$ freely generate $F$?
And the answer is no. For example, if $y' = (y x)^3 y^{-2}$, then $y' \equiv y$ after taking the quotient, but the pair $x, (y x)^3 y^{-2}$ don't generate the free group. This is believeable, but rather than being vague let's give a proof for completeness.
There is a group homomorphism $F \to S_3$ sending $x$ to the 2-cycle $(1 2)$ and $y$ to $(1 3)$. This homomorphism is surjective because these generate the group, but $y'$ maps to the trivial element because the image of $yx$ has order 3 and the image of $y$ has order 2. Therefore, $x$ and $y'$ don't generate $S_3$, and so they can't generate $F$.