test function and boundary condition For example, if we consider the Dirichlet energy $\int\frac 12 |\nabla u|^2$ and the solution space as follows:
$$X=\{u\in W^{1,2}(\Omega) \text{ | } u = 0 \text{ on } \partial\Omega \}$$
, then the test function is $C_0^\infty$. My question is that:
Is the test function related to the boundary condition $u = 0 \text{ on } \partial\Omega$?
If $u = g \text{ on } \partial\Omega$, should the test function be changed?

My understanding of your question is that you want to prove existence of a minimizer of the problem $\min_X I(u),$ where $I(u) = \int_{\Omega}\frac 12 |\nabla u|^2$. One way to approach the problem is by considering variations of the form $I(u + \epsilon\varphi)$, for $\varphi \in C^{\infty}_0(\Omega)$. Now what you should be careful about is that $u + \epsilon \varphi$ needs to belong to $X$ in order to get something that is meaningfully related to the problem we want to solve.
If you are now interested in minimizing over the space $Y = \{u \in W^{1,2} : \operatorname{Tr}(u) = g\}$, you should still consider variations of the form $u + \epsilon\varphi$, with $\varphi \in C^{\infty}_0$. Indeed $\operatorname{Tr(u + \epsilon\varphi)} = g + 0$ and hence $u + \epsilon\varphi \in Y$ if and only if $\operatorname{Tr}(\varphi) = 0.$
