Let $f:\mathbb{R}^n\to\mathbb{C}$ be a locally integrable function and let $p\in[1,+\infty)$ and $p'\in(1,+\infty]$ such that $\frac{1}{p}+\frac{1}{p'}=1$. Denoting by $C^\infty_c(\mathbb{R}^n)$ the set of infinitely differentiable functions of compact support, I proved via extension by continuity and density of a bounded linear operator, Riesz representation theorem and Hölder inequality that:
$$\|f\|_p=\sup_{\varphi\in C^\infty_c(\mathbb{R}^n),\\ \|\varphi\|_{p'}=1} \left|\int_{\mathbb{R}^n}f(x)\varphi(x)\operatorname{d}x\right|$$ without assuming a prior knowledge of the fact that $f\in L^p(\mathbb{R}^n)$ (i.e. if $f\notin L^p(\mathbb{R}^n)$ then RHS makes sense and it is equal to $+\infty$).
What if we remove the locally integrable hypothesis? I see that then we can't take the $\sup$ over all $C^\infty_c(\mathbb{R}^n)$ in general, because for some $\varphi\in C^\infty_c(\mathbb{R}^n)$ it could very well happen that the integral of $f\varphi$ is not well defined at all. So I thought that maybe if we take the $\sup$ over all $\varphi\in C^\infty_c(\mathbb{R}^n)$ such that $\varphi f\in L^1(\mathbb{R}^n)$ then what we obtain is again $\|f\|_p$. However, I didn't find a way to prove or disprove such a claim. So:
If $f:\mathbb{R}^n\to\mathbb{C}$ is a measurable function (not necessarily locally integrable), is it true that $$\|f\|_p=\sup_{\varphi\in C^\infty_c(\mathbb{R}^n),\\ \|\varphi\|_{p'}=1, \\ f\varphi\in L^1(\mathbb{R}^n)} \left|\int_{\mathbb{R}^n}f(x)\varphi(x)\operatorname{d}x\right| ?$$
I suppose that the function $f$ is measurable, which (as you clearly know) a little more general than $L^1_{\text{loc}}$-integrablity. I will assume that $f \geq 0$ too. So, we can use Lebesgue integral, which is defined as $ \int_{\Omega} f \,\, dx = \sup_{\phi \text{ is simple}} \sum_{i=1}^N a_i |E_i|$, $\,\,$ where $a_i$ is the value of $\phi$ for every $x\in E_i$, and the $E_i$'s are disjoint sets whose union, $\,\,\cup_{i=1}^N E_i = \Omega. \,$
Now, we have two cases:
Case 1: $\,\,f \in L^1_{\text{loc}}(\Omega)$, which is equivalent to saying that $\int_{K} f \,\, dx < \infty$ for every compact subset $K \subset \Omega$. This, I understand, is the case you have already discussed, and the formula works perfectly well.
Case 2: $\,\,f \notin L^1_{\text{loc}}(\Omega)$, which is to say that there is some compact subset $K \subset \Omega$ such that $\int_{K} f \,\, dx = \infty$. If this is the case, this means $\infty = \int_k f \,\,dx \leq \left(\int_K f^{p}\, \, dx\right)^{1/p} \underbrace{\left(\int_K 1 \, \, dx\right)^{1/p'}}_{\text{finite}}$. So, we already know that $||f||_p = \infty$. The problem is whether or not the formula will correctly depict the situation. I believe it does, which I argue as follows.
Let $K$ be the compact set above. Let $N \in \mathbb{N}$ and cover $K$ with the balls $B_{\delta(x)}(x)$ for $x \in K$, where $\frac{1}{N}>\delta(x) > 0$ are small enough for the balls to stay inside $\Omega$. Then, there is a finite subcollection $\{B_{\delta(x_1)}(x_1), ...,B_{\delta(x_k)}(x_k)\}$ that cover $K$. So, one of these balls --say, $B_{\delta(x_1)}(x_1)$-- satisfies $\int_{B_{\delta(x_1)}(x_1)} f \,\, dx =\infty$; otherwise, $\int_K f \,\, dx < \infty$. Take a smooth cutoff function, $0 \leq \xi_\epsilon\leq 1$, such that $\xi_\epsilon \equiv 1$ on $B_\delta = B_{\delta(x_1)}(x_1)$ and $\xi_\epsilon \equiv 0$ outside $B_{(1+\epsilon)\delta} = B_{(1+\epsilon)\delta(x_1)}(x_1)$. And set $c_\epsilon$ so that $\left(\int_{B_{(1+\epsilon)\delta}} (c_\epsilon \xi_\epsilon)^{p'}\,\,dx\right)^{1/p'} = 1$. Then, we have that $\int_{\Omega}f (c_\epsilon \xi_\epsilon)\,\,dx \geq c_\epsilon \int_{B_{\delta}} f\,\, dx = \infty$. So, the formula works as well.
$\,$
P.S. I apologize for the messiness.
$\,$ $\,$
Edit: I just saw your latest comment. I am not sure if the formula still work when $f$ has both negative and positive values, but I think it is possible.