Python + unittest + selenium = <3
- Python 3
- Selenium (already in requrements.txt)
- Virtualenvironment
- Docker
- Install
python3
- Install
virtualenv
$ pip3 install virtualenv
- Create a new virtualenv
$ virtualenv -p python3 your_env_name # env name can be anything
- Start the environment
$ source your_env_name/bin/activate
- Install dependencies in requirements.txt
$ pip install -r requirements.txt
- Install Chromedriver
$ wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
$ unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
- You're done!
All you need to do is:
python -m unittest
The unittest module can also be used from the command line to run single or multiple tests.
python -m unittest test1
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
Keep in mind that the files containing your tests need to be named test_{whatever}
so it can be found by unit test,
else you would have to pass the filename as an argument.
You have to build the image first
docker build . -t python-automation # you can name your image anything you want just remember it.
run it!
docker run -it python-automation
ImportError: Import by filename is not supported.
is caused by not using python3, most systems come pre-bundled with python2
You can make sure you have the right python version with
$ python --version
You might need to run commands using python3
try:
$ which python3
and it should have a path to your virtualenv python installation.
ModuleNotFoundError: No module named 'selenium'
You don't have not installed selenium dependency in your virtualenv or your system. Make sure you follow the setup guide.