Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You can customize and ignore unit tests with testcontainer #4586

Closed
liudaolunboluo opened this issue Oct 18, 2021 · 5 comments · May be fixed by #6445
Closed

You can customize and ignore unit tests with testcontainer #4586

liudaolunboluo opened this issue Oct 18, 2021 · 5 comments · May be fixed by #6445

Comments

@liudaolunboluo
Copy link

liudaolunboluo commented Oct 18, 2021

My requirements are as follows: my unit test integrates testcontainer, but I only want to run this unit test locally. When using Maven to build on the jekins of the server, I don't want to run unit tests with testcontainer, but I don't want to run all unit tests. Based on this scenario, I propose several solutions:

  1. Try catch has no exception in docker environment
  2. Judge whether there is a docker environment in the current running environment. If there is no docker environment, the unit test of testcontainer will not be executed. At present, I use the "docker - V" command in the code. If the docker version can be returned correctly, there is a docker environment. If there is no docker environment, there is no docker environment. However, the same problem as 1 is that if testcontainer runs locally for the first time and the docker environment is not installed, the developer may not know why this unit test class is not running
  3. I hope you can provide a configuration, parameter or option, such as "- dmaven. Skip. Test = true", which allows developers to decide whether to use testcontainer to perform unit tests in this Maven build
@rnorth
Copy link
Member

rnorth commented Oct 18, 2021

Hi @liudaolunboluo

There's already something for this:

  • firstly, have a look at the org.testcontainers.DockerClientFactory#isDockerAvailable method. This gives you a boolean result for whether Docker is available or not.
  • JUnit 4 and 5 both support 'Assumptions', which conditionally ignore tests. If you put something like assumeTrue(DockerClientFactory.isDockerAvailable()) in your test method, or in a before/setup method, you can skip tests that require Docker.

Of course, we'd always recommend running all tests in CI as well as locally, so I hope that you might also be successful in doing this too!

@liudaolunboluo
Copy link
Author

liudaolunboluo commented Oct 18, 2021

Hi @liudaolunboluo

There's already something for this:

  • firstly, have a look at the org.testcontainers.DockerClientFactory#isDockerAvailable method. This gives you a boolean result for whether Docker is available or not.
  • JUnit 4 and 5 both support 'Assumptions', which conditionally ignore tests. If you put something like assumeTrue(DockerClientFactory.isDockerAvailable()) in your test method, or in a before/setup method, you can skip tests that require Docker.

Of course, we'd always recommend running all tests in CI as well as locally, so I hope that you might also be successful in doing this too!

Thank you very much for your reply.

DockerClientFactory#isDockerAvailable () is not static. How do I call him in code?

@rnorth
Copy link
Member

rnorth commented Oct 18, 2021

Ah sorry, DockerClientFactory.instance().isDockerAvailable()!

@liudaolunboluo
Copy link
Author

DockerClientFactory.instance().isDockerAvailable()

Your reply is very helpful to me. Thank you very much

@rnorth
Copy link
Member

rnorth commented Oct 18, 2021

You're welcome!

@rnorth rnorth closed this as completed Oct 18, 2021
jtnord added a commit to jtnord/testcontainers-java that referenced this issue Jan 30, 2023
if code makes use of GenericContainer or any other TestContainer based
rule then those rules will mostl likely throw an exception when docker
is not available.

This leads people to add a BeforeClass annotation and test for docker,
however this does not skip the tests, it skips the class.  This has the
unfortuante side effect that the report (from thinks like maven surefire
and ant) that the test did not run when ingested into tools like Jenkins
(as zero tests from the class either passed failed or skipped).

it is also not obvious on the command line.

by using a rule the individual tests will be marked as skipped so it
becomes obvious that a test existed, but did not run for some reason
(and the reason will be in the exception - that docker is not available)

fixes testcontainers#4586 / testcontainers#343
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants