pexels-cottonbro-5473298
Run code from the terminal in python, goLang, Angular, java and C#

Why would you even want to call tests from the command line? especially if you can do this from your favourite IDE? What a wonderful question!

The reason are simple:

  • Add arguments to your tests.
  • No software licence required to run test.
  • Run tests via a scheduled task.
  • Run from a dockerized container.
  • Run tests, during continues integration (CI).
  • Run tests, before continues delivery (CD).
  • Multiple IDE’s are not required to run tests.
  • Hand off to a third party as part of fulfilment contract.

This is your quick start guide to running tests in java. First you will need a command window.

  • Windows: Click Start, type PowerShell, and then click Windows PowerShell.
  • Windows: Click Start, type cmd, and then click Command Prompt.
  • Mac: Click Launch Pad, Terminal.

C#

In c# the following command will run the tests in the command prompt. Please ensure you have required version of .net installed.

dotnet test

Mircrosoft .net command line execution can have the extra key features of:

  • Blame: debug why test runner itself crashing.
  • Verbosity: Varying degree of output from your tests.
  • Path: To the directory that holds your tests.
  • No build: Allows you to test old builds.
  • Results Directory: A directory where you can save and review test results.

The official Microsoft test page https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test. Where you will find more interesting ways to configure your test runner.

To get the help straight in the command line, run the following code:

dotnet test -h

GoLang

In Golang the following command will run the tests in the command prompt. Please ensure you have required version of Go installed.

go test

GoLang command line execution can have the extra key features of:

  • parallel: allows you to run test in parallel making execution faster
  • failfast: exit the test execution on the first test fail
  • shuffle: randomise the execution order of the tests
  • race: race condition detection

The official GoLang test page https://pkg.go.dev/cmd/go/internal/test#pkg-index . Where you will find more interesting ways to configure your test runner.

To get the help straight in the command line, run the following code:

go help test

Java

Java is a widely used language and many different flavours of build automation tools such as Maven and Gradle. Lets explore these as well as how to do it using a vanilla build without these tools.

mvn clean test

gradle test

# in java first compile and then run tests.
javac -d /[pathToClasses]/classes -cp /[pathToClasses/junit-4.12.jar /[pathToClasses]/[TestClassName].java

# run the Tests
4.12.jar:/[pathToCompiledClasses]/hamcrest-core-1.3.jar org.junit.runner.JUnitCore [package].[TestClassName]

You can see that a lot of effort has to be put into running a vanilla class without Maven or Gradle. If you can use a package manager, they will save you time and take the pain out of reference management. Including JUnit.

Gradle command line execution can have the extra key features of:

  • Fail fast: Terminates when it finds a broken test.
  • IgnoreFailures: Continues to build even if tests fails.
  • Max Parallel Forks: Runs test in multiple processes.

The official GoLang test page https://docs.gradle.org/current/userguide/java_testing.html . Where you will find more interesting ways to configure your test runner.

To get the help straight in the command line, run the following code:

gradle test -h

Maven Surefire command line execution can have the extra key features of:

  • Run a single test
  • rerunFailingTestsCount: Reruns test for a given number of times until it either passes or fails
  • debug: which allows you to step through your tests.

One key difference in this product compared to the rest is that you can set up some properties in the maven xml file such as excludes, setting environment vars etc

The official Maven test page is https://maven.apache.org/surefire/maven-surefire-plugin/ . Where you will find more interesting ways to configure your test runner.

To get the help straight in the command line, run the following code:

mvn test -help

Python

Python can use the module Unittest to run unit tests. This can also be run from the command line by using the following code from inside the project directory.

python -m unittest

Python unittest command line execution can have the extra key features of:

  • failfast: ends test cycle on the first test failure.
  • k: matches a pattern in selecting test to run
  • catch: can give an intermediate report on test run so far

The official Maven test page is https://docs.python.org/3/library/unittest.html. Where you will find more interesting ways to configure your test runner.

To get the help straight in the command line, run the following code:

python -m unittest -h

Python

Angular allows you to run tests at the component level. This is great, the ability to run unit tests on javascript code. The tests themselves are driven by karma.

To run tests you would use the following command:

ng test

Angular command line execution can have the extra key features of:

  • Browsers: allows you to set which browsers your test are executed against
  • code coverage: get an execution report
  • watch: run tests when code changes

The official Maven test page is https://angular.io/cli/test. Where you will find more interesting ways to configure your test runner.

To get the help straight in the command line, run the following code:

ng test --help

Although this is not an excessive list of the available arguments, the resources attached should help you find what you are looking for. Golang, Java, C#, Angular and Python all have some test capabilities in common. Some are passed in as arguments and others you must provide via a configured configurations file. 

If you found this article helpful, please consider buying a cup of coffee for a poor developer. You will brighten up his day and allow him to right more content like this one. Also, if you have any questions about what you have read then please feel free to reach out using [email protected].

Buy Now Button with Credit Cards