Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:01:17+00:00 2026-05-28T16:01:17+00:00

I have a test class that has a @BeforeClass method and a @Test method.

  • 0

I have a test class that has a @BeforeClass method and a @Test method. I am running the tests using Ant. I would like to know what is the best way to run only the @BeforeClass method, and not the @Test method. Here is the basic code layout:

@BeforeClass
public void init() throws Exception {
    /* Perform test initialization */
}

@Test
public void randomTest() throws Exception {
    /* Run the test */
}

I have tried making the @Test method part of a group, and then in Ant specifying the “excludedgroups” property to exclude that group. I have also tried setting the @Test method to be disabled. Neither of these methods worked because it wouldn’t run either the @Test or the @BeforeClass method.

One workaround I have is to make an empty @Test method, and then use either method above, but this does not seem like an elegant solution.

Is there a better way to run only the @BeforeClass method?

Edit:

Here is how I specify the task in my build.xml file:

<testng classpathref="test.classpath"
        outputdir="${reports}"
        haltonfailure="false"
        failureProperty="tests.failed"
        useDefaultListeners="true"                   listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter"
        parallel="classes"
        suitename="Tests"
        testname="${test.name} Tests">
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:01:19+00:00Added an answer on May 28, 2026 at 4:01 pm

    I ultimately decided to just put an if statement around every @Test method, and have the statement evaluate a System property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have a test class that has a @Resource annotation for a setter and
I have two classes: Action class, that has a method for executing VBScript files,
I have been looking at a class that has a method that accepts a
I have a simple FileCreator Ruby class that has 1 method create which creates
You have a method on a class that has 2 parameters, one of which
I have an abstract test class that has two type parameters. I need to
I have a class contain many variables, something like that class test { internal
I have a vector class that has it's Equals(object obj) method overridden so that
I'm using VS 2010 Professional built-in test cases. I've got a class that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.