DEV Community

Cover image for Lab8 Manage project complexity through the use of Automated Testing
Jason
Jason

Posted on

Lab8 Manage project complexity through the use of Automated Testing

The Lab8 are focused on managing project complexity through the use of Automated Testing. There are three frequently used framework for Python: Unittest, Nose and Pytest. Unittest has an associated module unittest2, but unittest2 is only applicable to Python 2.4-2.6. Nose has entered the maintenance mode. It can be seen from GitHub nose that the latest code submission of nose was on May 4, 2016. Nose2 inherits nose, but it should be noted that nose2 does not support all the functions of nose. Pytest's purpose is to make unit testing easier, and it can also be extended to support complex functional testing at the application level. Unlike unittest / nose, pytest can be used not only for unit testing, but also for higher-level, application-oriented functional testing. Therefore, if a higher level of testing is required, pytest is suitable. So I choose Pytest to test my project.

How to install

Run the following command in your command line:

  • pip install -U pytest

Check that you installed the correct version:

  • pytest --version

How to build directory tree

To get started writing tests, I need to simply create a folder named tests, which will contain test cases. Create an empty file called __init__.py in both tests folder and it's subfolder. Creating the __init__.py file means that the tests folder can be imported as a module from the parent directory. So my directory tree will look something like this:
Image description

How to write test case

In the assiLib.py has a function named get_txt_title. The function will specify if the article has title. If the first three lines have 2 blank lines and one non comment line, the function will take the non comment line as the title. I want to test it and separate it to 5 test cases. the first case is the title in the first line; the second is the title in the second line and it is centered; the third is the title in the Third line; the forth is the first line is the comment line; the fifth is the first line is the first sentence of the first paragraph.
Image description
When I run the test file, there are four passed and one failed. The failed case isn't a bug, it is an function need to be enhanced.
Image description

How to install Pytest-watch

What if you are working on a particular file/function, and want to run a single test over and over again while you fix a bug? There is a zero-config CLI tool that runs pytest, and re-runs it when a file in your project changes. It beeps on failures and can run arbitrary commands on each passing and failing test run. You can install it using the following command.

  • $ pip install pytest-watch You can run it using the every simple command in under the root directory.
  • $ ptw Image description

How to install Pytest-cov

This plugin produces coverage reports. Compared to just using coverage run this plugin does some extras:

  1. Subprocess support: you can fork or run stuff in a subprocess and will get covered without any fuss.
  2. Xdist support: you can use all of pytest-xdist's features and still get coverage.
  3. Consistent pytest behavior. If you run coverage run -m pytest you will have slightly different sys.path (CWD will be in it, unlike when running pytest).

Install it with pip:

  • pip install pytest-cov Usage:
  • pytest --cov=MAGIC-SSG tests/

Conclusion

I am looking for a job of automation tester. The lab8 is very helpful for me to interview the automation tester position.

Top comments (0)