Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Namah Shrestha
Namah Shrestha

Posted on

     

Chapter 3: Using PyTest, MyPy and flake8 for testing

3.1 Adding new dependencies to requirements

  • Now we need to install more libraries. Namelypytest,mypy andflake8.
  • So ourrequirements.txt file looks something like this. We have added dependencies forpytest:

    flake==3.9.4tox==3.24.3pytest==6.2.5pytest-cov==2.12.1mypy==0.910
  • Instead of directly overwriting therequirements.txt file, we create a new file calledrequirements_dev.txt. Since testing is required indev environment.

  • So we have two requirement files:

    • requirements.txt which we use while running our code:

      requests==2.26.0
    • requirements_dev.txt which holds dev dependencies like our test, lint and type check libraries.

      flake==3.9.4tox==3.24.3pytest==6.2.5pytest-cov==2.12.1mypy==0.910
  • While running tests we will userequirements_dev.txt file to install dependencies.

3.2 Adding to our metadata insetup.cfg

  • Oursetup.cfg file looks something like this:

        [metadata]    name = something    description = just some dummy codebase    author = Coding with Zim    license = MIT    license_file = LICENSE    platforms = unix, linux, osx, cygwin, win32    classifiers =         Programming Language :: Python :: 3        Programming Language :: Python :: 3 :: Only        Programming Language :: Python :: 3.6        Programming Language :: Python :: 3.7        Programming Language :: Python :: 3.8        Programming Language :: Python :: 3.9    [options]    packages =        something    install_requires =        requests>=2    python_requires = >=3.6    package_dir =        =src    zip_safe = no    [options.extras_require]    testing=        pytest>=6.0        pytest-cov>=2.0        mypy>=0.910        flake8>=3.9        tox>=3.24    [options.package_data]    something = py.typed    [flake8]    max-line-length = 160
  • options.extras_require are optional dependencies. We can read more about it here:https://setuptools.pypa.io/en/latest/userguide/dependency_management.html

  • options.package_data is a type ofData File Support , we can read more about it here:https://setuptools.pypa.io/en/latest/userguide/datafiles.html. We are basically using it to denote that our code istype-hinted, which means we are using types in the code.

  • For this to work we need to create apy.typed blank file in our something directory alongside__init__.py.

  • flake8 is the configuration for theflake8 linter that we will be using. Here we are only saying that the maximum line length is 160.

  • So ourflake8 configuration is stored in thecfg file and ourpython configuration is stored in ourpyproject.toml file.

3.3 Adding python configurations topyproject.toml

  • Ourpyproject.toml file looks something like this:

    [build-system]requires=["setuptools>=42.0", "wheel"]build-backend="setuptools.build_meta"[tool.pytest.ini_options]addopts="--cov=something"testpaths=["tests",][tool.mypy]mypy_path="src"check_untyped_defs=truedisallow_any_generics=trueignore_missing_imports=trueno_implicit_optional=trueshow_error_codes=truestrict_equality=truewarn_redundant_casts=truewarn_return_any=truewarn_unreachable=truewarn_unused_configs=trueno_implicit_reexport=true
  • We already discussed about thebuild-system.

  • tool.pytest.ini_options arepytest tool options during the execution of tests. In our case we have added the--cov=something option inaddopts so that we see the test coverage.testpaths basically denotes all the directories containing tests.

  • tool.mypy aremypy tool options. To learn aboutmypy:https://realpython.com/lessons/type-checking-mypy/#:~:text=“Mypy is an optional static,background on the Mypy project.

  • So, now we install thedev dependencies withpip:

    (env)yourproject$pipinstall-r requirements_dev.txt
  • Finally we should be able to run our test withpytest, check linting withflake8 and type check withmypy.

    $pytest# this command will test our code with default test discovery mechanism.$mypy# this command will run type checks on our code with mypy.$flake8# this command will check code linting.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Backend Engineer | Software Engineer
  • Location
    Bangalore
  • Education
    Sikkim Manipal Institute of Technology (B.Tech - Information Technology)
  • Work
    Senior Software Engineer at Biofourmis
  • Joined

More fromNamah Shrestha

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp