An equivalent condition for a measure to be invariant

Why is it true that for a compact metric space $X$ and a continuous function $T:X\rightarrow X$, a measure $\mu$ on $X$ is $T$- invariant iff $\int_X f\circ T \, d\mu=\int_X f \, d\mu$ for every continuous function $f$? Is it still true without demanding that $X$ is compact or metric?

-

Fix $F\subset X$ closed; we can find a sequence of continuous function $\{f_n\}$ with $0\leq f_n\leq 1$ and $\lim_{n\to +\infty}f_n(x)=\chi_F(x)$ for each $x\in X$ (define for example $f_n(x):=\frac{d(x,O_n^c)}{d(x,F)+d(x,O_n^c)}$, where $O_n:=\{x\in X, d(x,F)<n^{—1}\}$). This gives, by dominated convergence, that $\mu(T^{-1}F)=\mu(F)$ for each closed set $F$. This is true for each open.
Now, let $A\in\mathcal B(X)$ and $\varepsilon>0$. We can find $O$ open, $O$ containing $A$ such that $\mu(O\setminus A)<\varepsilon$. Then $\mu(O)\leq \mu(A)+\varepsilon$. Since $T^{-1}(O)$ is open (as $T$ is continuous), we have $$\mu(T^{-1}(A))\leq \mu(T^{-1}(O))=\mu(O)\leq \mu(A)+\varepsilon$$ hence $\mu(T^{-1}(A))\leq \mu(A)$. Applying the same argument to $X\setminus A$ we get what we want.
Here, we use the fact that $X$ is compact in order to ensure us that continuous functions are bounded (if it's not a compact space, we can find a continuous unbounded non-negative function, hence we are not sure it's integrable). We used the fact that we worked with a metric space in order to approach a Borel set by an open.