Having trouble using regular expressions with package name matching

63 views
Skip to first unread message

Rich MacDonald

unread,
Mar 8, 2024, 1:53:46 PMMar 8
to testng-users
I am struggling to figure out the regular expression matching for package names.

Say I want to include only package names that end with "test".

com.mycompany.packagename.test
com.mycompany.packagenametest

I assume the matching for this is:

<packages>

<package name=".*test" />

</packages>


But this doesn't work. I've searched but am not finding examples that explain my error. Thanks.

I am also interested in filters for class names, e.g., to only examine classes whose name ends in "Test". I am aware of the beanshell option, but if I use beanshell then i also have to include the package name filtering in beanshell, afaik.

Rich MacDonald

Krishnan Mahadevan

unread,
Mar 9, 2024, 12:54:41 AMMar 9
to testng...@googlegroups.com
Rich,

I dont think the <package> name directive supports regex. From looking at the codebase, I was able to find support only for “.*” as suffix to indicate that all sub-directories need to be considered as well.

As you mentioned, you should be using a beanshell for this instead.

Here’s an example of the beanshell that can be used.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="2980_suite" verbose="2">
    <test name="2980_test" verbose="2">
        <method-selectors>
            <method-selector>
                <script language="beanshell">
                    <![CDATA[
                    pkgname = testngMethod.getConstructorOrMethod().getDeclaringClass().getPackageName();
                    return pkgname.matches(".*test");
            ]]>
                </script>
            </method-selector>
        </method-selectors>
        <packages>
            <package name="com.rationaleemotions.pkg.*"/>
        </packages>

    </test>
</suite>
➜  pkg pwd
~/testng_playground/src/test/java/com/rationaleemotions/pkg
➜  pkg tree .
.
├── Parser.java
├── packagename
│   └── test
│       └── SampleTestCase.java
└── packagenametest
    └── SampleTestCase.java

4 directories, 3 files


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/9b7fdf9d-3060-410d-9e10-d64fdf14f9a0n%40googlegroups.com.

Rich MacDonald

unread,
Mar 9, 2024, 1:05:33 PMMar 9
to testng-users
Hi Krishnan,

I agree. I needed to read the documentation more closely. So regular expressions are used for groups, methods, dependsOnGroups and dependsOnMethods but not classes and packages?

I am not familiar with the codebase. How easy is it to add regex to class and package scanning? Mostly I am concerned with performance implications. It would be nice to quit further scanning at the package and class level, vs having to use reflection to cycle through the list of methods.

Thanks
Rich MacDonald

Krishnan Mahadevan

unread,
Mar 9, 2024, 1:40:19 PMMar 9
to testng...@googlegroups.com
Rich

You might want to start to debug from 


This is the class that is responsible for translating package names into loadable classes. 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

From: testng...@googlegroups.com <testng...@googlegroups.com> on behalf of Rich MacDonald <macdona...@gmail.com>
Sent: Saturday, March 9, 2024 11:35:32 PM
To: testng-users <testng...@googlegroups.com>
Subject: Re: [testng-users] Having trouble using regular expressions with package name matching
 

Krishnan Mahadevan

unread,
Mar 9, 2024, 2:05:26 PMMar 9
to testng...@googlegroups.com
Heres the right link 


Previously i shared the link from my forked repo. My bad. 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

From: Krishnan Mahadevan <krishnan.ma...@gmail.com>
Sent: Sunday, March 10, 2024 12:10:08 AM
To: testng...@googlegroups.com <testng...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages