Repository: conan-io/conan

Issue:
[question] test_pacakge_folder doesn't work for conan export-pkg
### What is your question?

Hi,

I’ve started using the `test_package_folder` attribute to define a custom test package folder in my recipes. Everything works as expected when I call `conan create`, i.e. the package tests are executed. However, if I call `conan build` followed by `conan export-pkg`, the tests are not executed – the `conan export-pkg` command finishes after creating the package. I find this surprising because if I place the package test in the default folder, i.e. `test_package` (and remove `test_package_folder` attribute), the tests are executed.

On the other hand documentation mentions only `conan create` so I'm not sure if it's intended behaviour or a bug:
> The test_package_folder class attribute allows defining in recipe a different default test_package folder for `conan create` commands. 



### Have you read the CONTRIBUTING guide?

- [x] I've read the CONTRIBUTING guide


Discussion:
Hi @bkarasm 

Thanks for your feedback!

The ``test_package_folder`` was introduced indeed as a ``conan create`` feature.
I have checked it, and I think it makes sense that behaves consistently for ``conan export-pkg`` too, let me try adding it for next Conan 2.19 release
Thanks!
Seems easy to implement, PR in https://github.com/conan-io/conan/pull/18621, lets see if there are no downsides or unexpected side effects, but it seems it will be ok. Thanks!



Patch:
diff --git a/conan/cli/commands/export_pkg.py b/conan/cli/commands/export_pkg.py
index aa9d5591781..06b7936715b 100644
--- a/conan/cli/commands/export_pkg.py
+++ b/conan/cli/commands/export_pkg.py
@@ -38,7 +38,6 @@ def export_pkg(conan_api, parser, *args):
 
     cwd = os.getcwd()
     path = conan_api.local.get_conanfile_path(args.path, cwd, py=True)
-    test_conanfile_path = _get_test_conanfile_path(args.test_folder, path)
     overrides = eval(args.lockfile_overrides) if args.lockfile_overrides else None
     lockfile = conan_api.lockfile.get_lockfile(lockfile=args.lockfile, conanfile_path=path,
                                                cwd=cwd, partial=args.lockfile_partial,
@@ -92,6 +91,9 @@ def export_pkg(conan_api, parser, *args):
     lockfile = conan_api.lockfile.update_lockfile(lockfile, deps_graph, args.lockfile_packages,
                                                   clean=args.lockfile_clean)
 
+    test_package_folder = getattr(conanfile, "test_package_folder", None) \
+        if args.test_folder is None else args.test_folder
+    test_conanfile_path = _get_test_conanfile_path(test_package_folder, path)
     if test_conanfile_path:
         from conan.cli.commands.test import run_test
         # same as ``conan create`` the lockfile, and deps graph is the one of the exported-pkg


Test patch:
diff --git a/test/integration/layout/test_layout_generate.py b/test/integration/layout/test_layout_generate.py
index 26ed44f379e..05723e77371 100644
--- a/test/integration/layout/test_layout_generate.py
+++ b/test/integration/layout/test_layout_generate.py
@@ -155,3 +155,11 @@ def test_custom_test_package_sibling(self):
                 "pkg/my/test/conanfile.py": GenConanfile().with_test("self.output.info('MYTEST!')")})
         c.run("create pkg/conan")
         assert "MYTEST!" in c.out
+
+    def test_custom_test_package_export_pkg(self):
+        c = TestClient(light=True)
+        conanfile = GenConanfile("pkg", "0.1").with_class_attribute('test_package_folder="mytest"')
+        c.save({"pkg/conanfile.py": conanfile,
+                "pkg/mytest/conanfile.py": GenConanfile().with_test("self.output.info('MYTEST!')")})
+        c.run("export-pkg pkg")
+        assert "MYTEST!" in c.out