pgrx
pgrx copied to clipboard
Way to debug tests?
How do I attach a debugger to test code?
It's pretty easy to do it for regular code. I do "cargo pgx run pg14", let psql start, and then in my IDE I pick "Attach to Process", find the psql process, and then on the command line I run a script to trigger the code.
For test code, though, the system doesn't pause to allow attaching a debugger.
Is there some trick I'm missing?
(I'm using Clion + lldb on a Mac, if that matters.)
I'm sorry, I don't use a debugger very often. Usually my understanding is that Eric does, and usually he starts with a core dump of some sort to get that going, but that sort of requires a segfault in the first place to generate, as I understand (and that is where he finds debuggers useful), and inducing that is kind of a bad idea usually, so... my apologies.
Hmmm I think if you built the extesion with the pg_test feature on you could do like cargo pgx run --features pg_test then presumably you could run the generated test functions from psql where you do have time to set the debugger. (I'd check cargo pgx schema for the test name to see what that is)
I can't promise this will work though. :sweat_smile:
@Hoverbear Yes, this does work.
You have to do two things. First, you have to create a #[pg_extern] function outside the test module so that you have a way of calling the test code from psql. The second thing is to add "pub" to the function within the test module.
#[pg_extern]
fn run_the_test() {
tests::my_test();
}
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
#[pg_test]
pub fn my_test() {
// place breakpoint here in the code to test
}
}
It's a little awkward because you have to remember to delete run_the_test() later, but that's ok.
Way cool solution. Thank you.
I'll leave this issue open in case you want to document it.