PyCharm 2018.2.2

PyCharm 2018.2.2 is now available, with some small improvements. Get it now from our Website. If you’re still on PyCharm 2018.1, we’ve also got a release candidate for our new bugfix update PyCharm 2018.1.5

New in 2018.2.2

  • Some improvements to our pipenv support: if the pipfile specifies packages which aren’t compatible with your computer, they will no longer be suggested. Also, if you choose to create a pipenv for a project you’ve already opened, the project’s dependencies will now automatically be installed. This matches the behavior of pipenv on the command-line.
  • A regression where virtualenvs weren’t automatically detected has been resolved.
  • Some issues in version control support were ironed out: when you right-click a commit to reword it (change its commit message) in some cases PyCharm wasn’t able to identify the new hash of the commit correctly, this has been cleared up.
  • And much more, see the release notes for details.

Interested?

Get PyCharm from the JetBrains website

If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm, and stay up to date. You can find the installation instructions on our website.

PyCharm 2018.1.5 RC

If you’re using PyCharm 2018.1, and for some reason can’t upgrade to PyCharm 2018.2, we also have a new version for you. The release candidate for PyCharm 2018.1.5 can be downloaded from Confluence.

In this release, we’ve fixed an issue where unshelving files would create a large number of threads. If you’re interested in learning about other issues that were resolved, check out the release notes here.

Posted in Release Announcements | Tagged | Leave a comment

PyCharm 2018.2.2 RC

PyCharm 2018.2.2 Release Candidate is now available, with some small improvements. Get it now from our Confluence page

New in This Version

  • Some improvements to our pipenv support: if the pipfile specifies packages which aren’t compatible with your computer, they will no longer be suggested. Also, if you choose to create a pipenv for a project you’ve already opened, the project’s dependencies will now automatically be installed. This matches the behavior of pipenv on the command-line.
  • A regression where virtualenvs weren’t automatically detected has been resolved.
  • Some issues in version control support were ironed out: when you right-click a commit to reword it (change its commit message) in some cases PyCharm wasn’t able to identify the new hash of the commit correctly, this has been cleared up.
  • And much more, see the release notes for details.

Interested?

Download the RC from our confluence page

If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm RC versions, and stay up to date. You can find the installation instructions on our website.

The release candidate (RC) is not an early access program (EAP) build, and does not bundle an EAP license. If you get PyCharm Professional Edition RC, you will either need a currently active PyCharm subscription, or you will receive a 30-day free trial.

Posted in Early Access Preview | Tagged | Leave a comment

PyCharm and pytest-bdd

Last week we published a blog post on the new pytest fixture support in PyCharm 2018.2. This feature was a big part of the 2018.2 work and proved to be a hit. But it wasn’t the only notable pytest work: 2018.2 also delivered support for behavior testing with pytest-bdd, a project that provides behavior testing with pytest as the test framework. Our What’s New in 2018.2 video gave a quick glimpse of this support.

Let’s rewrite the tutorial from last week, showing the pytest-bdd support instead of simply working with fixtures. Behavior-driven-development (BDD) support is a part of PyCharm Professional Edition, previously covered in a blog post about using Behave and an older blog post about BDD. This tutorial puts pytest-bdd to use.

Want the finished code? It’s in available in a GitHub repo.

What and Why for BDD

If you’re new to BDD, it can appear a bit strange. You write a programming-free specification, known as a “feature”, that describes how the software is supposed to behave. You then implement that specification with test code.

This two-step process shines in several workflows:

BDD can seem like overhead. But if you find yourself lost in your test code, the feature spec can help you step back from the details and follow the high-level intent. Even better, when you come back to your code months later, it is much easier to get up-to-date. Finally, if you feel committed to the “Given/When/Then” test style, pytest-bdd moves that commitment from “dating” to “marriage”.

Setup

We’ll use the same example application as the previous blog post.

To follow along at home, make sure you have Python 3.7 (our example uses dataclasses) and Pipenv installed. Clone the repo at https://github.com/pauleveritt/laxleague and make sure Pipenv has created an interpreter for you. (You can do both of those steps from within PyCharm.) Make sure Pipenv has installed pytest and pytest-bdd into your interpreter.

Then, open the directory in PyCharm and make sure you have set the Python Integrated Tools -> Default Test Runner to pytest, as done in the previous blog post.

This time, though, we also need to set Languages & Frameworks -> BDD -> Preferred BD framework to pytest-bdd. This will enable many of the features discussed in this tutorial. More information is available in the PyCharm Help on pytest-bdd.

Now you’re ready to follow the material below. Right-click on the tests directory and choose Run ‘pytest in tests’. If all the tests pass correctly, you’re setup.

Let’s Make a Feature

What parts of our project deliver business value? We’re going to take these requirements, as “features”, and write them in feature files. These features are specified using a subset of the Gherkin language.

Right click on the tests folder and select New, then choose Gherkin feature file.
Name the file games. Note: Some like to group BDD tests under tests/features.

Here’s the default file contents generated by PyCharm:

Let’s change the feature file’s specification to say the following:

This Scenario shows the basics of BDD:

  • Each Feature can have multiple Scenarios
  • Each Scenario has multiple steps
  • The steps revolve around the “given/when/then” approach to test specification
  • Step types can be continued with the And keyword
  • Given specifies inputs
  • When specifies the logic being tested
  • Then specifies the result

We might have a number of other scenarios relating to games. Specifically, the “winner” when the score is tied. For now, this provides enough to implement.

As we type this feature in, we see some of the features PyCharm Professional provides in its Gherkin support:

  • Syntax highlighting of Gherkin keywords
  • Reformat Code fixes indentation
  • Autocomplete on keywords
  • Warnings on unimplemented steps

Implement The Steps

If you run the tests, you’ll see….no difference. The games.feature file isn’t a test: it’s a specification. We need to implement, in test code, each of the scenarios and scenario steps.

PyCharm can help on this. As mentioned above, PyCharm warns you about an “Undefined step reference”:

If you Alt-Enter on the warnings, PyCharm will offer to either Create step definition or Create all steps definition. Since 2018.2 does the latter in an unexpected way (note: it’s being worked on), let’s choose the former, and provide a File name: of test_games_feature:

Here’s what the generated test file test_games_feature.py looks like:

These are just stubs, of course, which we’ll have to come back and name/implement. We could implement the other steps by hand. Let’s instead let PyCharm continue generating the stubs, into the same file.

2018.2 doesn’t generate the scenario, which is what actually triggers the running of the test. Let’s provide the scenario, as well as implement each step, resulting in the following for the test_games_feature.py implementation:

To see this in action, let’s run the test then take a look at what pytest-bdd is doing.

Run the Tests

You’ve already run your regular pytest tests, with fixtures and the like. What extra does it take to also run your pytest-bdd tests? Nothing! Just run your tests:

Your pytest-bdd tests show up just like any other test.

Let’s take a look at some things pytest-bdd is doing:

  1. The @scenario decorated function test_games_feature is the only function in the file with the test_ prefix. That means, this file only has one test. And guess what? The function itself doesn’t do anything. It’s just a marker.
  2. We need two teams, so we implement the two Given steps by making a Blue and Red team.
  3. We also need a game between these two teams. This is the third step in our games.feature scenario. Not that this function takes two arguments. As it turns out, pytest-bdd steps are pytest fixtures which can be injected into the functions for each step. (In fact, any pytest fixture can be injected.)
  4. Now that we have a game setup, we use @when to run the logic being tested by recording a score.
  5. Did our logic work? We use @then to do our assertion. This is where our test passes or fails.

It’s an interesting approach. It’s verbose, but it clearly delineates the “Given/When/Then” triad of good test cases. Plus, you can read the almost-human-language Gherkin file to quickly understand what’s the behavior being tested.

Test Parameters

You might have noticed that the feature file specified a score for the game but it was ignored in the implemented tests. pytest-bdd has a feature where you can extract parameters from strings. It has several parsing schemes. We’ll use the simplest, starting by adding parsers to the import from pytest_bdd:

Note: You could also do this the productive way by using the symbol and letting PyCharm generate the import for you.

The feature file says this:

Let’s change our @when decorator to parse out the score:

When we do so, PyCharm warns us that Not all arguments.... were used. You can type them in, but PyCharm knows how to do it. Hit Alt-Enter and accept the first item:

After changing record_score to use these values, our function looks like this:

As we experiment with lots of combinations, moving this to the feature file is very helpful, particularly for test engineers.

Want more? Gherkin and pytest-bdd support “Scenario Outlines” where you can batch parameterize your inputs and outputs, just like we saw previously with pytest.mark.parametrize.

Use pytest Fixtures and Features

As noted above, pytest-bdd treats scenario steps as pytest fixtures. But any pytest fixture can be used, which makes pytest-bdd a powerful approach for BDD.

For example, imagine we have an indirection where there are different types of Game implementations. We’d like to put the choice of which class is used behind a fixture, in conftest.py:

We can then use this fixture in the @given step when we instantiate a Game:

Productive BDD

Prior to 2018.2, PyCharm provided BDD for Behave, but nothing for pytest-bdd. Admittedly, there are still loose ends being worked on. Still, what’s available now is quite productive, with IDE features brought to bear when getting in the pytest-bdd flow.

Warnings

Forgot to implement a step? Typo in your @scenario text identifier? PyCharm flags these with the (configurable) warning and error infrastructure.

Autocomplete

Obviously we autocomplete keywords from Gherkin and pytest-bdd. And as expected, we autocomplete function names as fixture parameters. But we also autocomplete strings from the feature file…for example, to fix the kind of error just mentioned for the scenario identifier. Also, parameters parsed out of strings can autocomplete.

Quick Info

Wonder what is a particular symbol and where it came from? You can use Quick Info or on-hover type information with Cmd-hover to see more information:

Conclusion

Doing behavior-driven development is a very different approach to testing. With pytest-bdd, you can keep much of what you know and use from the very-active pytest ecosystem when doing BDD. PyCharm Professional 2018.2 provides a “visual testing” frontend to keep you in a productive BDD flow.

Posted in Tutorial | Tagged , | 5 Comments

PyCharm 2018.2.1 Out Now

The latest version of PyCharm is now available: get PyCharm 2018.2.1 from our website now.

New in This Version

  • An issue on Linux where part of the window wouldn’t be redrawn correctly has been resolved
  • A performance problem that affected Django users with a MacBook Pro with a touch bar has been fixed
  • And more: read the release notes

Any Comments?

If you have questions, remarks, or complaints, please reach out to us! You can ask our support team for assistance, create tickets in our issue tracker, follow us on Twitter, or just leave a comment on this blog post.

Posted in Release Announcements | Tagged | Leave a comment

PyCharm 2018.2 and pytest Fixtures

Python has long had a culture of testing and pytest has emerged as the clear favorite for testing frameworks. PyCharm has long had very good “visual testing” features, including good support for pytest. One place we were weak: pytest “fixtures”, a wonderful feature that streamlines test setup. PyCharm 2018.2 put a lot of work and emphasis towards making pytest fixtures a pleasure to work with, as shown in the What’s New video.

This tutorial walks you through the pytest fixture support added to PyCharm 2018.2. Except for “visual coverage”, PyCharm Community and Professional Editions share all the same pytest features. We’ll use Community Edition for this tutorial and demonstrate:

  • Autocomplete fixtures from various sources
  • Quick documentation and navigation to fixtures
  • Renaming a fixture from either the definition or a usage
  • Support for pytest’s parametrize

Want the finished code? It’s in available in a GitHub repo.

What are pytest Fixtures?

In pytest you write your tests as functions (or methods.) When writing a lot of tests, you frequently have the same boilerplate over and over as you setup data. Fixtures let you move that out of your test, into a callable which returns what you need.

Sounds simple enough, but pytest adds a bunch of facilities tailored to the kinds of things you run into when writing a big pile of tests:

  • Simply put the name of the fixture in your test function’s arguments and pytest will find it and pass it in
  • Fixtures can be located from various places: local file, a conftest.py in the current (or any parent) directory, any imported code that has a @pytest.fixture decorator, and pytest built-in fixtures
  • Fixtures can do a return or a yield, the latter leading to useful teardown-like patterns
  • You can speed up your tests by flagging how often a fixture should be computed
  • Interesting ways to parameterize fixtures for reuse

Read the documentation on fixtures and be dazzled by how many itches they’ve scratched over the decade of development, plus the vast ecosystem of pytest plugins.

Tutorial Scenario

This tutorial needs a sample application, which you can browse from the sample repo. It’s a tiny application for managing a girl’s lacrosse league: Players, Teams, Games. Tiny means tiny: it only has enough to illustrate pytest fixtures. No database, no UI. But it includes those things needed for actual business policies that make sense for testing.

Specifically: add a Player to a Team, create a Game between a Home Team and Visitor team, record the score, and show who won (or tie.) Surprisingly, it’s enough to exercise some of the pytest features (and was actually written with test-driven development.)

Setup

To follow along at home, make sure you have Python 3.7 (it uses dataclasses) and Pipenv installed. Clone the repo at https://github.com/pauleveritt/laxleague and make sure Pipenv has created an interpreter for you. (You can do both of those steps from within PyCharm.)

Then, open the directory in PyCharm and make sure you have set the Python Integrated Tools -> Default Test Runner to pytest.

Now you’re ready to follow the material below. Right-click on the tests directory and choose Run ‘pytest in tests’. If all the tests pass correctly, you’re setup.

Using Fixtures

Before getting to PyCharm 2018.2’s (frankly, awesome) fixture support, let’s get you situated with the code and existing tests.

We want to test a player, which is implemented as a Python 3.7 dataclass:

It’s simple enough to write a test to see if the id was constructed correctly:

But we might write lots of tests with a sample player. Let’s make a pytest fixture with a sample player:

Now it’s a lot easier to test construction, along with anything else on the Player:

We can get more re-use by moving this fixture to a conftest.py file in that test’s directory, or a parent directory. And along the way, we could give that player’s fixture a friendlier name:

The test would then ask for mary instead of player_one:

We’ll go back to the simple form of player_one, without name=’mary’, for the rest of this tutorial.

Autocompletion

When you really get into the testing flow, you’ll be cranking out tests. The boilerplate becomes a chore. It would be nice if your tool both helped you specify fixtures and visually warn you when you did something wrong.

First, make sure you configure PyCharm to use pytest as its test framework:

When writing the test_player test above, player_one is a function argument. It also happens to be a pytest fixture. PyCharm can autocomplete on pytest test functions and provide known fixture names. So as you start to type pla, PyCharm offers to autocomplete:

PyCharm is smart about this list, because it isn’t an editor just matching strings. Ever get bugged by the “Indexing….” time of PyCharm? Well, here’s your payback. PyCharm knows what are valid symbols inside that pytest-flavored function. For example, in the code block of the test function, type di and see that PyCharm autocompletes on dir, the Python built-in function:

Now put the cursor in the test function’s arguments and type di. You get a different, context-sensitive list. One without dir, but with names of known fixtures — in this case, built-into pytest:

So there you go, next time someone says PyCharm is “heavy”, just think also of the word “productive”. PyCharm works hard to semantically discover what should go in that autocomplete, from across your code and your dependencies.

What’s That Fixture?

Big test code bases mean lots of fixtures in lots of places. You might not know where Mary put that fixture. Hell, in a month, you won’t know where you put that fixture. PyCharm has several ways to help without pushing you out of your test flow.

For example, you see a test:

You’re not sure what is player_one. Put your cursor on it and press F1 (Quick Info). PyCharm gives you an inline popup with:

  • The path to the file, as a clickable link
  • The function definition
  • The function return value, as a clickable link
  • The docstring…rendered as HTML, for goodness sake

And it does all this without interrupting your flow. You didn’t have to hunt for the file, or do a “find” operation high up in your project with tons of false positives. You didn’t have a dialog to dismiss. You got the answer, no muss no fuss. It’s this commitment to “flow” that distinguishes PyCharm for serious development.

If you want to just jump to that symbol, put your cursor on player_one and hit Cmd-B (Ctrl+B on Windows & Linux). PyCharm will open that file with the cursor on the definition, even if the argument referenced a named fixture.

Refactor -> Rename

As you refactor code, you refactor tests. And as you refactor tests, you refactor fixtures. Over, and over, and over….

PyCharm does some of the janitorial work for you. For example, renaming a fixture. In your test function, put your cursor on the player_one argument and hit Ctrl-T (Ctrl+Alt+Shift+T on Windows & Linux), then choose Rename and provide player_uno as the new name. PyCharm confirms with you all the places in your code where it found that symbol (not that string, though it can do that too), letting you confirm with the Do Refactor button.

This action found the definition and all the usages, starting from a usage. You could also start in conftest.py with the definition and refactor rename from there.

“Wait, player_uno is wrong, put it back to player_one!” you might say. Piece of cake. The IDE treated all of that as one transaction, so a single Undo reverts the renaming.

Note: Refactor Rename doesn’t work on fixtures defined with the name parameter

Using Parametrize

Let’s look at another piece of pytest machinery: parametrize, that oddly-named pytest.mark function. It’s useful when you want to run the same test, with different inputs and expected results.

For example, our lacrosse league application tells us which team won a game, or None if there was a tie:

Rather than write one test to see if home wins and another to see if visitor wins, let’s write one test using parametrize:

PyCharm has support for parametrize in several ways.

Foremost, PyCharm now autocompletes on @pytest.mark itself, as well as the mark values such as parametrize.

Next, if you omit one of the parameters, PyCharm warns you:

Then, when you go to fill in the parameter arguments, PyCharm autocompletes on the remaining unused parameters:

Finally, PyCharm correctly infers the type of the parameter, which can then help typing in the test function. Below we see the popup from Cmd-Hover (meaning, hover over a symbol and press Cmd):

Conclusion

Getting into the test flow is important and productive. PyCharm’s support for pytest has long been a help for that and the fixture-related additions in 2018.2 are very useful improvements.

For more on pytest in general, see our friend Brian Okken’s Python Testing with pytest book (I’ve read it multiple times) and Test and Code podcast. The PyCharm help has a good page on pytest (including the new fixtures and pytest-bdd support.)

If you have any questions on this tutorial, feel free to file them as a comment on this blog post or in the the repo’s Issues.

Posted in Tutorial | Tagged | 6 Comments

PyCharm 2018.2.1 RC Out Now

PyCharm 2018.2.1 Release Candidate is now available, with various bug fixes. Get it now from our Confluence page

Improved in This Version

Interested?

Download the RC from our confluence page

If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm RC versions, and stay up to date. You can find the installation instructions on our website.

The release candidate (RC) is not an early access program (EAP) build, and does not bundle an EAP license. If you get PyCharm Professional Edition RC, you will either need a currently active PyCharm subscription, or you will receive a 30-day free trial.

Posted in Early Access Preview | Tagged | 6 Comments

PyCharm 2018.2 Out Now

PyCharm 2018.2 is now available: Pipenv environments, pytest support improvements, new and improved quick documentation, and much more

 

New in PyCharm

  • PyCharm now fully supports pipenv: easily create projects with pipenv, create a pipfile, and have PyCharm update it for you when you install packages. If you’re checking out a project from GitHub with a pipfile, a pipenv will be created automatically, and you can install all dependencies with one click.
  • Support for pytest has been improved in PyCharm 2018.2: code completion and inspections are now support for pytest fixtures. If you’d like to use BDD, pytest-bdd is now supported as well. Please note that BDD support is available only in PyCharm Professional Edition.
  • Quick documentation has been redesigned to be easier to use and also just to look better. Don’t have docs yet? The reStructuredText editor now comes with a preview window, making editing rst files a lot easier.

Further code insight, version control, and other improvements have been made, read the What’s New page to learn more. Already convinced? Then download PyCharm now

Do you have questions, complaints, or suggestions? Please reach out to us! Ask questions to our support team, report bugs and suggestions on our issue tracker, or just connect with us on Twitter.

 

Posted in Release Announcements | Tagged | 5 Comments

PyCharm 2018.2 RC 2

Today we’re making the second release candidate for PyCharm 2018.2 available on our website. We’ve only made one small fix to how usage statistics are collected.

Usage statistics collection in PyCharm is opt-in and completely voluntary. It greatly helps us to see which functionality in PyCharm actually gets used, and which areas should receive the most attention for new features. To all users who opt-in: thank you very much!

See the blog post of the previous RC to read what’s new since the last EAP.

Interested?

Download the RC from our website. Alternatively, you can use the JetBrains Toolbox App to stay up to date.

If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm RC versions, and stay up to date. You can find the installation instructions on our website.

The release candidate (RC) is not an early access program (EAP) build, and does not bundle an EAP license. If you get PyCharm Professional Edition RC, you will either need a currently active PyCharm subscription, or you will receive a 30-day free trial.

Posted in Early Access Preview | Tagged | 3 Comments

PyCharm 2018.2 Release Candidate

PyCharm 2018.2 is getting closer to its release date, and we’re happy to announce that a release candidate is available for download now.

New in PyCharm

  • The dot in pytest is no more! If you’re looking for ‘Py.test’, it’s now known as ‘pytest’ anywhere in PyCharm.
  • Fixtures for pytest-BDD are now supported. We’ve added a lot of pytest and pytest-BDD features in 2018.2. If you’d like to play around with BDD, make sure to get the Professional Edition of PyCharm
  • In some cases SQL queries were falsely identified as f-strings, and f-strings in regexes were marked as errors. Both of these false positives have been fixed
  • Code completion for Pandas DataFrames in the debug console was slow, and should now work as expected.

Read the release notes to learn more.

Interested?

Download the RC from our website. Alternatively, you can use the JetBrains Toolbox App to stay up to date.

If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm RC versions, and stay up to date. You can find the installation instructions on our website.

The release candidate (RC) is not an early access program (EAP) build, and does not bundle an EAP license. If you get PyCharm Professional Edition RC, you will either need a currently active PyCharm subscription, or you will receive a 30-day free trial.

Posted in Early Access Preview | Tagged | 8 Comments

PyCharm 2018.2 EAP 7

PyCharm 2018.2 EAP 7 is out! Get it now from the JetBrains website.

In this EAP we fixed lots of bugs in various subsystems and integrated features and bug-fixes recently added to WebStorm and DataGrip.

Read the Release Notes

Interested?

Download this EAP from our website. Alternatively, you can use the JetBrains Toolbox App to stay up to date throughout the entire EAP.

If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm EAP, and stay up to date. You can find the installation instructions on our website.

PyCharm 2018.2 is in development during the EAP phase, therefore not all new features are already available. More features will be added in the coming weeks. As PyCharm 2018.2 is pre-release software, it is not as stable as the release versions. Furthermore, we may decide to change and/or drop certain features as the EAP progresses.

All EAP versions will ship with a built-in EAP license, which means that these versions are free to use for 30 days after the day that they are built. As EAPs are released weekly, you’ll be able to use PyCharm Professional Edition EAP for free for the duration of the EAP program, as long as you upgrade at least once every 30 days.

 

Posted in Early Access Preview | Tagged | 3 Comments