how do you write unit tests in python for a java software? the idea is to automate unit tests for a java program but the automation framework works with python. is it even possible?
-
Unit tests are intended to verify the functionality of a block of code and require access to the module. So the answer is NO. However, since you use the words automate and automation framework - I am guessing you are actually intending to test a feature - integration tests. In that case, you can always invoke code from your python to simulate the feature call and test the behavior. If it's web app, you invoke the URL endpoint in python for various inputs and verify the output. – bschandramohan Feb 23 '20 at 01:34
1 Answers
Just about anything is possible in theory. But some things are not practical.
First of all, if you are talking about Jython rather than Python, this is relatively straight-forward:
(But also see When will Jython support Python 3? ... given that Python 2 is now "end of life".)
The rest of this post assumes that you are talking about "real" Python.
Unit tests for a Java program mean that you are calling methods in the application code base. Your unit tests typically use a mocking framework to create mock objects to simulate other parts of the application. The unit tests and the mocking framework are normally implemented in Java.
If you were to implement your unit tests as python methods, your python unit test methods would be making multiple cross-language calls from Python to Java. In the case of the mock objects, the application code (under test) would be making cross-language calls from Java back to Python.
Call going in one direction are possible, though there are all sorts of messy things that you need to worry about when mapping values between Python & Java, and other things. Calls going in both directions? I don't know if it is even practical1.
In short: this will be too difficult to achieve. It will be more efficient (of your time and your co-workers time) to implement your Java unit tests in Java.
The above is for a conventional Java application unit testing. What about testing a Java web application?
- You can write Selenium tests in Python; see https://selenium-python.readthedocs.io/getting-started.html
- You can write tests for a RESTful API in Python. (There is probably an existing Python framework for doing this, but I didn't look.)
These should work no matter what language the webapp is implemented in. But I would argue that they are not "unit testing" in the conventional meaning of that term.
Finally, if you only want to be able to run some Java unit tests from Python, that should be easy. Just work out how to run your Java unit test framework's launcher from the command line. Then do that from Python.
You could in theory do it at a finer grained level, but in practice it would be hard. A Python runner for Java JUnit tests would need to be able to identify the Java classes that contain various JUnit annotations in order to work out what the tests are. (I've not heard of
1 - It is possible in theory. All you need to develop implementations of Java and Python that support this. It is "just" a few man years of dev effort :-).

- 698,415
- 94
- 811
- 1,216